Why This Matters
Binary search is the canonical algorithm. It runs faster than the time it takes to read its own input out loud — a billion-element array takes 30 comparisons, not a billion. Understanding why halving works, and why the implementation is so easy to break, is a prerequisite to every divide-and-conquer algorithm that follows.
Jon Bentley reported in Programming Pearls (1986) that 90% of professional programmers, asked to implement binary search after a binary-search lecture, introduced a bug. Joshua Bloch found in 2006 that the standard textbook implementation had a latent integer-overflow bug that broke Arrays.binarySearch in the Java standard library and bsearch in C. The lesson: the algorithm is short, the proof is short, and the implementation is still easy to get wrong.
Specification
Input. A sorted array in non-decreasing order, and a target value .
Output. Either an index with , or the report that no such index exists.
Cost. comparisons. Termination guaranteed.
The Algorithm
Maintain two indices (low) and (high) such that the target, if present, lies in . Initialize . At each step, compute the midpoint (more on this below). Compare against :
- If , return .
- If , the target is in the upper half: set .
- If , the target is in the lower half: set .
Continue while . If the loop exits, the target is not in the array.
The Loop Invariant
The correctness of binary search rests on a single invariant maintained at every iteration:
If is in , then is in .
Initially , so is the whole array — the invariant trivially holds. At each step, the comparison at either finds (we return) or eliminates exactly half of the current range. The invariant is preserved because the eliminated half cannot contain given the sortedness of . When the loop exits with , the range is empty, so was not in the array.
The Midpoint Trap
The textbook midpoint formula is
This is wrong on real machines. If exceeds the maximum integer width (typically for 32-bit signed ints), the addition overflows and the result wraps around to a negative number. Indexing A[m] with a negative index is undefined behavior in C, an ArrayIndexOutOfBoundsException in Java, a crash in most languages.
The fix, attributed to Bloch (2006):
This is mathematically equivalent for non-negative but does not overflow. Use this form. The bug it fixes was latent in standard libraries for over twenty years before Bloch noticed it.
Lower-Bound and Upper-Bound Variants
In practice you often want not "is in the array" but "where would go?" or "what is the first index ?" These are the lower-bound and upper-bound variants. Pseudocode for lower bound (smallest with ):
lo, hi = 0, n
while lo < hi:
m = lo + (hi - lo) / 2
if A[m] < t: lo = m + 1
else: hi = m
return lo // n if all elements < t
The differences from plain binary search:
- starts at , not . The answer can be (target larger than every element).
- The loop condition is , not .
- The else branch sets , not . Closing the half-open interval .
Use lower-bound when you need to insert into a sorted array, look up by floor or ceiling, or count occurrences. C++ STL std::lower_bound and Python's bisect.bisect_left both implement this variant.
Common Mistakes
Off-by-one in the loop condition. Plain binary search uses <= h; lower-bound uses < h. Mixing them creates infinite loops or skipped elements.
Forgetting the half-open vs closed interval distinction. Plain binary search works on (closed). Lower-bound works on (half-open). These need different update rules. Keep one in mind at a time.
The midpoint overflow — discussed above. If you ever write (lo + hi) / 2 in production code, this page should make you uncomfortable.
Searching an unsorted array. Binary search requires sortedness. The algorithm gives wrong answers, not "not found," on unsorted input. If you cannot prove the input is sorted, sort it first or use linear search.
References
- Bentley, J. Programming Pearls (2nd ed., 2000). Ch. 4 — "Writing Correct Programs."
- Bloch, J. (2006). "Extra, Extra — Read All About It: Nearly All Binary Searches and Mergesorts are Broken." Google Research Blog.
- Cormen, Leiserson, Rivest, Stein. Introduction to Algorithms (4th ed., 2022). §2.3.5 — Binary search.
- Knuth. The Art of Computer Programming, Volume 3: Sorting and Searching (2nd ed., 1998). §6.2.1 — Binary tree search.
Next Topics
- Big-O notation — the asymptotic-analysis foundation that makes "" a precise claim.
- Arrays and pointers — the random-access substrate binary search depends on.