Simple Search vs Binary Search
Suppose I have a shelf full of steelbooks (special edition movie cases) lined up in no particular order. Someone asks: do I own "The Dark Knight"?
I ordered the steelbooks from most liked to least liked.
I start at one end and check every single steelbook, one by one, until I find "The Dark Knight" or until I've gone through the entire shelf. That's the whole algorithm. It's dull, but honest: if there are n movies, in the worst case I look at all of them.
People call this linear search, or simple search. I like "simple" better. It doesn't pretend to be clever.
Now imagine someone suggests: start with the steelbook in the middle.
If the movie in the middle comes after "The Dark Knight" alphabetically, I can skip the whole right half of the shelf. If it comes before, I can skipp the left half. Repeat. With each step, half the remaining titles vanish without you ever picking them up.
That is binary search. A shelf of a million steelbooks collapses in about twenty checks. A billion? Still about thirty. The effort doesn't scale so much with shelf length as with the digits in its length.
Simple search grows with the shelf. Binary search grows with the logarithm of the shelf.
At first, it might seem like binary search is just a fancier way to do the same thing as simple search, only faster. But actually, that's not true—and that's the main confusion.
The hidden contract
Binary search isn't a smarter way to scan the same shelf.
It's an entirely different deal.
You can discard half the steelbooks only because the shelf is sorted. The middle title means something only when everything to its left is alphabetically "before," and everything to its right is "after." Without that rule, the middle steelbook is just another case—opening it tells you nothing about the rest of your collection.
Simple search and binary search play by different rules. Simple search works anywhere, for example the list can be chaos. Binary search needs order up front. It's not a fair fight at all because they're solving different problems with different contracts.
So when we write O(n) vs O(log n), we're not comparing two recipes for the same kitchen. We're comparing:
- Simple search: works on any shelf. Pays per title.
- Binary search: works only on ordered shelves. Pays fewer checks because the order did work for you up front.
Binary search doesn't find movies faster in a vacuum. It uses fewer checks only because someone already sorted the collection.
That “someone” might have been you, on Sunday, alphabetizing the shelf. Or maybe you always shelve new arrivals in the right spot to keep things ordered forever. The logarithmic magic is real but the cost you pay upfront.
Pretend the shelf is messy
Here's the naive version people write in interviews, and it's perfectly fine:
function simpleSearch(list, target) {
for (let i = 0; i < list.length; i++) {
if (list[i] === target) return i;
}
return -1;
}
No assumptions. The shelf can be chaos. "The Dark Knight" might be first, last, or missing.
Binary search needs a sorted shelf. It looks more like this:
function binarySearch(sortedList, target) {
let low = 0;
let high = sortedList.length - 1;
while (low <= high) {
const mid = Math.floor((low + high) / 2);
const guess = sortedList[mid];
if (guess === target) return mid;
if (guess < target) low = mid + 1;
else high = mid - 1;
}
return -1;
}
You're discarding half the search space. You just never said who sorted the shelf, or when, or how often you have to do it again.
If my steelbooks arrive haphazardly and I only need to find "The Dark Knight" once, sorting everything first just to use binary search is a weird flex. Sorting costs more than a single walk down the shelf for most practical cases. I ordered everything just to avoid having to look at it.
If I'm going to search this shelf a thousand times, alphabetizing once (or shelving new arrivals the right way from the start) starts to make sense. Now, binary search is only part of the story. The real story is pay for order up front, and reap logarithmic gains forever.
The Value in Both Approaches
Argue for simple search for a second.
Simple search works anywhere. You don’t need to sort the list or set anything up in advance. It’s great when you can’t control the order, or when the list is short—like a small menu or a handful of movies. For small lists, just walking through is usually fast enough, and sometimes faster, even if binary search sounds "fancier." Big-O tells you how things scale, not who wins with a tiny list.
But what if your list is huge and already sorted? Then it’s wasteful not to use binary search. Skipping the fast way and checking every item doesn’t make sense. Things like sorted shelves or database indexes exist for a reason: they let you find what you want quickly, over and over.
The binary was false all along. The real question was never “which algorithm is better?” It was: what have I already guaranteed about the shape of my collection?
What the logarithm is really saying
People memorize: linear is O(n), binary is O(log n). That's a handy flashcard.
But the heart of it is: log n is the number of times you can halve a collection until nothing's left to check. That only works when each cut is valid. Validity comes from ordering.
So “use binary search” is incomplete advice. Complete advice is more like:
- What questions will I ask, and how often?
- Can I pay for a structure that lets me cut away huge sections with each check?
- Who maintains that structure as the collection changes?
Sometimes the answer is a sorted array and binarySearch. Sometimes it's an object with average O(1) lookup, different contract: equality, not order. Sometimes, it's “just walk the shelf; you have forty steelbooks.”
The shelf, again
Back to the steelbooks.
If they're scattered and I need "The Dark Knight" once, I walk the shelf.
If they're shelved alphabetically and I need "The Dark Knight" often, I jump to the middle and keep halving what's left, again and again, until I know the truth.
Binary search feels like magic until you spot the trick: the magician sorted the movies before you sat down.
Halving only works if someone already lined up the cases.