Searching
Algorithms
Master linear search and binary search: understand how each works, analyze their time complexity, and know when to apply each approach.
What Is Searching?
Searching is the process of locating a target value within a data structure. In AP Computer Science A, you work primarily with arrays and ArrayLists. A search algorithm inspects elements until it either finds the target or determines it is absent.
Why Efficiency Matters
Imagine searching for a name in a phone book with 1,000,000 entries. A linear scan could require up to 1,000,000 comparisons. Binary search needs at most \( \lceil \log_2 1{,}000{,}000 \rceil = 20 \) comparisons: a dramatic difference.
Key Vocabulary
Linear Search: Step by Step
Linear Search: Java Code
Binary Search: Divide and Conquer
Binary search exploits sorted order. Compare the target to the middle element. If the target is smaller, search the left half; if larger, search the right half. Repeat until found or the range is empty.
Binary Search: Java Code
A sorted collection makes discarded search ranges meaningful
Sorted order makes it possible to discard an entire search range after one comparison. An unsorted collection does not support the same elimination.Binary search with Java indices
Search this sorted nine-value array for 23. The tested midpoint values are 17, 31 and 23; Java indices 0 through 8 lead to a return value of 5. Each failed comparison excludes its tested midpoint.Count comparisons as the list grows
For inclusive midpoint binary search on n ≥ 1 items, the maximum number of probes is floor(log₂ n) + 1. The horizontal axis uses a log₂ scale; the count measures search probes after sorting.Binary Search: Complexity
For an array of 1,024 elements, binary search needs at most \( \log_2 1024 = 10 \) comparisons. Linear search could need up to 1,024.
Linear vs. Binary: Key Distinction
| Linear Search | Binary Search | |
|---|---|---|
| Precondition | None | Sorted array |
| Best Case | \( O(1) \) | \( O(1) \) |
| Worst Case | \( O(n) \) | \( O(\log n) \) |
| Approach | Sequential scan | Divide and conquer |
| Works on unsorted? | Yes | No |
| Implementation | Simple loop | Track low, mid, high |
How to Choose an Algorithm
Example 1: Linear Search Trace
Example 2: Binary Search Trace
Sorted array: [2, 5, 8, 12, 15, 23, 31, 42, 50]. Target: 8.
Example 3: Counting Comparisons
An array has \( n = 1{,}000 \) sorted elements. What is the maximum number of comparisons for binary search?
Common Mistakes
Summary Comparison
| Linear Search | Binary Search | |
|---|---|---|
| Time (Best) | \( O(1) \) | \( O(1) \) |
| Time (Worst) | \( O(n) \) | \( O(\log n) \) |
| Sorted Required? | No | Yes |
| Implementation | Single for-loop | While-loop with low/high/mid |
| \( n = 1{,}000{,}000 \) | Up to 1,000,000 steps | Up to 20 steps |
| When to Use | Unsorted or small data | Sorted and large data |
Question 1 of 5
What is the worst-case time complexity of binary search on a sorted array of \( n \) elements?
Question 2 of 5
Which statement about linear search is true?
Question 3 of 5
In the binary search code below, what happens if the loop condition is while (low < high) instead of while (low <= high)?
Question 4 of 5
A sorted array has 2,048 elements. What is the maximum number of comparisons binary search will make?
Question 5 of 5
Given the sorted array [3, 7, 11, 18, 24, 30] and target 18, what is the sequence of values at mid that binary search examines?