Why Binary Search Beats Linear Search on Sorted Data
Byte stands at a towering wall of numbered lockers arranged in perfect sorted order, holding a notepad and tapping the middle locker with a stylus, eyes narrowed in concentration as rows of lockers stretch left and right into the distance.
- Explain how binary search eliminates half the remaining candidates at each step.
- Compare the number of steps binary search versus linear search requires on a sorted list of n items.
- Calculate the maximum number of comparisons binary search needs given a list size using log base 2.
- Identify the precondition that binary search requires and explain why that precondition does not apply to linear search.
- Predict how doubling the list size affects the worst-case step count for each algorithm.
Key terms
- Linear search
- Scanning elements one at a time from start until the target is found.
- Binary search
- Repeatedly halving a sorted range by comparing the target to the middle element.
- Search space
- The set of remaining candidate positions still possibly containing the target.
- Precondition
- A requirement that must hold before an algorithm runs correctly, such as sortedness.
- Logarithmic growth
- Step count that increases by one each time the input size doubles.
The Halving Invariant
Binary search maintains a key invariant: the target, if present, always lies within the current low-to-high range. Each step inspects the middle element and uses the sorted order to discard the half that provably cannot contain the target, preserving the invariant on a range half the size. Because the range shrinks geometrically, the number of steps to reach a single candidate is log₂(n). This invariant reasoning is also how you prove correctness: as long as the discard rule never removes the target, the search either finds it or correctly reports absence.
Why Sortedness Is Non-Negotiable
The entire speed advantage of binary search rests on the precondition that data is sorted. On a sorted list, 'target is less than middle' guarantees the target cannot be in the upper half, so discarding it is safe. On unsorted data that guarantee evaporates: the target could sit anywhere, so discarding a half can throw away the very element you seek, producing a wrong 'not found'. Linear search imposes no ordering requirement, which is why it remains correct on any list at the cost of O(n) time.
Worked examples
Trace binary search for target 61 in the sorted list [3,7,12,19,25,31,38,44,51,58,61,67,74,80,88,95].
- Set low=0, high=15; middle index = (0+15)//2 = 7, value 44. 61 > 44, so search the right half: low=8.
- Now low=8, high=15; middle = (8+15)//2 = 11, value 67. 61 < 67, so search the left half: high=10.
- Now low=8, high=10; middle = (8+10)//2 = 9, value 58. 61 > 58, so search right: low=10.
- Now low=10, high=10; middle = 10, value 61. Match found at index 10 after four comparisons.
Answer: Found 61 at index 10 in 4 comparisons, versus 11 comparisons for linear search.
Activity
Drag and run both search algorithms on the sorted list below to compare how many comparisons each needs to find the target value 61.
Practice
How many worst-case comparisons does binary search need on a sorted list of 4096 items?
Explain why binary search may report a missing value as not found on unsorted data.
Common mistakes to avoid
- Binary search works on any listBinary search requires sorted data; on unsorted lists it can discard the half holding the target.
- Doubling the list doubles binary-search workDoubling the input adds only one extra halving step because the growth is logarithmic.
Check your understanding
A sorted list contains 1,024 elements. In the worst case, how many comparisons does binary search need to find an element?
A developer uses binary search on a list and consistently gets wrong results. Which precondition is most likely violated?
A sorted list doubles in size from 1,000 to 2,000 items. How does this affect the worst-case number of comparisons for binary search versus linear search?
Recap
Binary search halves a sorted search space each comparison, running in O(log n), while linear search scans every element in O(n). Binary search's speed depends entirely on the sorted precondition that linear search never requires.
Reflect
When would you still choose linear search even though binary search is asymptotically faster?