Binary Search: The Math Nobody Actually Derives (With a Live Race)
Not just how it works — why the math works. The integer overflow trap hiding in the naive formula, the full O(log n) derivation from first principles, and a live animation to see both searches race.
- The textbook midpoint formula (low+high)/2 can integer-overflow on large arrays — this exact bug lived in Java for 9 years.
- The safe formula is low + (high-low)/2 — algebraically identical, but the intermediate sum can never overflow.
- O(log n) isn't just intuition — it falls straight out of solving n / 2^k = 1 for k.
- A billion-element array takes at most 30 comparisons; that asymptote is the whole point of algorithm design.
I learned binary search theory today. Not the "here's the code, now memorize it" version — the actual math underneath it.
Two things bothered me going in: why does the standard midpoint formula have a production bug, and how do you actually prove — not just assert it? Here's everything I worked through, with interactive demos so the intuition sticks.
What Binary Search Actually Does
You have a sorted array. You want to find a target value. Instead of scanning left to right, you:
- Check the middle element
- If it matches — done
- If the target is smaller — search the left half
- If the target is larger — search the right half
- Repeat until found or the search space is empty
Each step cuts the problem in half. The code is simple. The math is interesting.
Part 1: The Bug in the Formula You Were Taught
The "obvious" midpoint formula:
This is mathematically correct. It will also crash your program on large arrays.
The problem is integer overflow. If and are both close to the maximum value a 32-bit integer can hold (~2.1 billion), their sum exceeds what the variable can store. You get a negative number or garbage — not a crash with a clear error message, just silently wrong behavior.
This isn't hypothetical. This exact bug lived in Java's Arrays.binarySearch() for nine years before Joshua Bloch caught it in 2006.
The safe formula:
Subtract first, then add. is always smaller than either number alone, so it can't overflow.
But are these actually the same? Here's the algebraic proof:
They compute the exact same midpoint. The safe version just sidesteps the intermediate sum that causes the overflow.
Part 2: Why ? The Full Derivation
Most explanations just say "it halves the array each time, so it's logarithmic." That's the intuition — here's the actual proof.
Setup:
- Let = number of elements in the sorted array
- Let = number of iterations in the worst case
The halving pattern:
Each step reduces the search space by half:
| Iteration | Search space |
|---|---|
The derivation:
Worst case: we keep halving until exactly 1 element remains. Set the search space at step equal to 1:
Multiply both sides by :
Apply to both sides:
Use the logarithmic power rule — :
Since :
The maximum steps to search elements is . Drop the base for Big-O notation: .
What does this feel like in practice?
| Array size (n) | Linear steps | Binary steps k = log₂(n) | Ratio |
|---|---|---|---|
| 8 | 8 | 3 | 3× |
| 64 | 64 | 6 | 11× |
| 1,024 | 1,024 | 10 | 102× |
| 1,000,000 | 1,000,000 | 20 | 50,000× |
| 1,000,000,000 | 1,000,000,000 | 30 | 33,333,333× |
A billion-element array takes at most 30 comparisons. That's the real magic — not the code, but what the math guarantees.
Part 3: Watch It Race
Derivations are one thing. Here's the visual:
Try different targets. Target 14 (near the end) shows the gap most clearly — linear search grinds through 14 comparisons while binary search jumps to the answer in 3.
Target 4 (near the beginning) is linear search's best case — worth seeing how the algorithms compare when linear actually has a short path.
The Three Things Worth Internalizing
1. Sorted is the prerequisite. Binary search only works because you can eliminate half the search space based on a comparison. Unsorted data: you're back to linear.
2. The overflow bug isn't trivia. The naive formula fails in production on large arrays. The safe formula is what you should actually write — and now you can prove why it's equivalent.
3. Logarithms grow impossibly slowly. . A problem larger takes only more steps. That asymptote is the whole game in algorithm design.
The code is five lines. The math behind it earns that simplicity.
Keep reading
We Shipped an AI Code Reviewer With Three Prompts. It Was Wrong Too Often and Quiet Too Long.
2026-07-28 · 9 min readOne Reviewer, Four Codebases, Four Different Definitions of Correct
2026-07-28 · 10 min readOur Cross-File Pass Couldn't See Other Files. Tree-sitter Fixed That.
2026-07-28 · 10 min readWe Put a Cheap Model in Charge of the Expensive Ones
2026-07-28 · 10 min read