Somewhere behind every search bar, autocomplete, and database index you've ever used sits an idea a child already knows: when someone says “guess my number, I'll say higher or lower,” you don't start at 1. You start in the middle. Every answer — higher, lower — destroys half of everything that's left. That's the entire algorithm. The rest of this essay is just watching that one move, very closely, until it stops looking like a trick.
Play it first. Then we'll slow the machine down to one line of code at a time.
Slide and guess. The shaded region is where my number can still be hiding. Par is 7.
Six billion humans play this game as children. Your move.
§1Now watch the interpreter play it
Ten lines of code play the same game against a sorted shelf of numbers. Below, you get the view the machine has: the exact line being executed, every variable it's juggling, and the shelf with its live pointers — lo, hi, and their midpoint mid. Faded books are provably eliminated: the code will never look at them again.
Click a book to make it the target (or take the unlucky pick), then ⏭ step through the code line by line.
| lo | — |
| hi | — |
| mid | — |
| arr[mid] | — |
| target | 42 |
| comparisons | 0 |
| still possible | 15 books |
lo = mid + 1 is the code saying “everything left of here is now impossible” — it never re-checks, never doubts. Four comparisons, maximum, for fifteen books (⌈log₂ 15⌉ = 4). And the unlucky pick matters: the loop ends with lo crossing past hi — the honest way to say “not here” without checking every book.
(lo + hi) >> 1 is integer halving. Fun scar tissue: for ~20 years most real-world implementations carried an overflow bug in exactly this line.
§2The part that feels illegal
Fifteen books is cute. The reason binary search runs the world is what happens when the shelf stops being cute. Halving doesn't care how big the pile is — it only cares how many times the pile can be halved. And that number grows absurdly slowly.
Drag the shelf size and watch the two strategies race — at one check per second, human speed.
§3The fine print that bites
Binary search's power comes from one assumption so load-bearing it's easy to forget: the shelf must be sorted. Every elimination — “the target can't be left of here” — is only true if order holds. Break the assumption and the algorithm doesn't crash, doesn't warn, doesn't slow down. It does something much worse: it answers quickly and wrongly.
The number 23 is on this shelf — you can see it. Unsort the shelf, run the search, and watch the machine swear it isn't there.
§4What to keep
- Binary search = the kid's guessing game, formalized: halve your ignorance every step.
- The cost is log₂ n — a billion items, thirty questions. Doubling the data costs one more question.
- The two pointer moves are proofs of elimination, not guesses — which is exactly why the sorted precondition is sacred.
- Broken preconditions don't crash; they produce fast, confident garbage. Check them.
This is the third essay in a row where the villain was confident wrongness — a model without sources, a loop without limits, a search without its precondition. The pattern is the lesson: speed and confidence are cheap; verified answers are engineering.
← previously: № 1 · How RAG finds the answer · № 2 · Inside the loop