What is binary search — and how to find the commit that broke everything in 10 steps

Yesterday everything worked. Today the "Pay" button won't click. Between yesterday and today there are forty commits — from you and from your AI agent.
Checking them one by one kills the evening. But there's a way to find the culprit in six checks. It's called binary search, and you've already used it. You just didn't know its name.
What binary search is
Binary search is a way to find something in a sorted list by throwing away half of it every time.
Remember the game "guess a number from 1 to 100"? A bad player asks: "Is it 1? 2? 3?". A good one asks: "Is it more than 50?". A "yes" — and half the options are gone. Next: "More than 75?". And so on to the end.
The good player guesses any number in 7 questions. The bad one might need a hundred.
In code it looks like this. You have an array sorted in ascending order. Look at the middle:
- the middle equals what you want — found it;
- what you want is smaller — keep searching only the left half;
- it's bigger — only the right half.
Repeat until you find it or run out of halves.
Why it's so fast
Every step cuts the problem in half. And halving eats even huge numbers very quickly.
- 100 items — at most 7 steps.
- 1,000 — 10 steps.
- 1,000,000 — 20 steps.
- A billion — about 30.
A plain loop over a million items might do a million comparisons. Binary search does twenty. Add another million items — and binary search needs just one extra step.
That's why database lookups feel instant even on giant tables. A database index works on a similar principle: data is kept in order, and the search cuts away big chunks at a time.
The key condition: order
Binary search works only on sorted data. That's not a formality.
If the list is shuffled, "smaller than the middle" means nothing. The item may sit in the half you threw away. The search won't crash — it'll just quietly say "not found", even though the item is right there.
This is a common trap in AI-written code. The search function is correct, but the data came from the server in random order. So the rule is simple: before searching by halves, make sure everything is sorted by the same field you're searching on.
Surprise: it's really hard to write correctly
The idea fits in one sentence. The code doesn't.
Programmer Jon Bentley ran an experiment: he gave professionals the task of writing a binary search. By his account, about 90% of the solutions had a bug. Most often an off-by-one boundary — and the search loops forever or skips the last item.
Worse: there was a bug even in Java's standard library. The line mid = (low + high) / 2 overflowed on very large arrays and broke the search. The bug lived for about nine years until it was found in 2006.
The takeaway: don't hand-write binary search if a ready one exists. Many languages have it built in. Python has the bisect module. Java has Arrays.binarySearch. And in JavaScript, includes or find is plenty for everyday tasks — binary search matters when you truly have a lot of data.
Where it helps you today: git bisect
Back to the broken button. Git has a command that runs binary search over your commit history.
- Start the search:
git bisect start. - Say the current state is broken:
git bisect bad. - Name a commit where it definitely worked:
git bisect good a1b2c3d. - Git checks out a commit in the middle. You test the button and answer
git bisect goodorgit bisect bad. - After a few answers Git prints: "this is the first bad commit".
- Go back:
git bisect reset.
Forty commits — at most six checks. A thousand — ten. And if you have a test that catches the bug, you don't even have to answer by hand: git bisect run npm test runs the whole thing for you.
Found the commit — now look at what exactly it changed and decide: undo it or fix it.
The same trick works without Git. Can't tell which part of the code breaks the page? Comment out half. Still broken — the culprit is in the remaining half. Fixed — it's in the half you commented out. That's manual binary search, and it's almost always faster than guessing.
How is binary search different from linear search?
Linear search checks items one by one, start to finish. Binary search jumps to the middle and throws away half. Linear doesn't need order; binary absolutely does.
If I have to sort first, isn't that slower?
For a single search — yes, sorting costs more than a plain scan. Binary search pays off when you search the same data many times: sort once, and every search after that is almost free.
Why is it called "binary"?
From "two": at every step you choose between two halves. It's also called half-interval search or logarithmic search.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





