Simple Search vs Binary Search
Imagine a shelf filled with steelbooks (special edition movie cases) that I've arranged from my favorites to least favorites. Someone asks if I own "The Dark Knight."
To find "The Dark Knight," I start at one end and check each steelbook one by one until I find it or reach the end of the shelf. That's the whole method: simple, straightforward, and in the worst case I check all n movies.
This is called linear search or simple search. I prefer simple because it doesn't pretend to be clever.
As the collection grows, it becomes hard to find a steelbook if it's located at the end.
One of my friends who knows this pain recommends ordering the shelf alphabetically. He reasons that if the middle movie title comes after "The Dark Knight" alphabetically, I can ignore the entire right half. If it comes before, I skip the left half. Then repeat. Each step lets me eliminate half of what's left—no need to look at every case.
This is binary search. On a shelf with one million steelbooks, you'll find your answer in about twenty checks. For a billion, about thirty checks. The work scales with the number of digits in the collection size, not the size itself.
Simple search scales with the shelf size. Binary search scales with the logarithm of the shelf size.
It's easy to think binary search is just a faster version of simple search. But that's not accurate, even though it's a common assumption.
The hidden contract
Binary search isn't merely a smarter way to check the same shelf.
It's a totally different game.
You get to skip half the shelf only because the shelf is sorted. The middle steelbook has meaning only when everything to its left is alphabetically "before," and everything to its right is "after." If that's not true, the middle tells you nothing about the rest.
Simple and binary search follow different rules. Simple search works everywhere, even in chaos. Binary search needs order from the start. They don't really compete directly; they address different problems with different contracts.
So when we compare O(n) to O(log n), it's not two approaches to the same problem. It's more like
Simple search: Works anywhere. Checks each title.
Binary search: Works only with a sorted shelf. Uses fewer checks because someone already did the organizing.
Binary search isn't quick in a vacuum. It only uses fewer checks if someone put in the work to organize everything first.
Maybe you sorted the shelf last weekend. Maybe you always put each new arrival in place right away. The real win is paid up front.
Pretend the shelf is messy
Here's the basic version most people write during interviews, and it's fine:
function simpleSearch(list, target) {
for (let i = 0; i < list.length; i++) {
if (list[i] === target) return i;
}
return -1;
}There are no assumptions. "The Dark Knight" could be anywhere, or missing.
Binary search requires a sorted shelf. It's more like this:
// .. some searching algorithm
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 slicing the search space in half each time. But you never specified who sorted the shelf, when, or how often you have to do it again.
If my steelbooks come in randomly and I only need to find "The Dark Knight" once, it actually takes more work to sort the shelf just to run binary search. Sorting takes more effort than a single walk through, so it wouldn't be worth it.
But if I need to search this shelf over and over, alphabetizing it once (or sorting as I add new movies) makes sense. Now, binary search is only part of the story—the upfront cost pays off over many searches.
The Value in Both Approaches
Simple search just works without any need to organize or prep beforehand. If you can't control the order, or if your list is really short—a menu or a handful of movies—it's probably faster to just look through the whole thing. For small lists, setup costs usually outweigh any binary search speedup. Big O is about how things scale, not which method is fastest when you only have six cases.
On the other hand, if your list is big and already sorted, ignoring binary search is wasteful. Sorted shelves and database indexes exist for a reason: they let you find things fast, especially when you search a lot.
The question isn't "which search is better?" It's What structure do I have, or can I afford to create, for my data?
What's logarithm really telling us?
People remember that linear search is O(n) and binary search is O(log n). Handy shorthand.
But the core idea: log n is how many times you can halve a set before nothing's left. That only works if each split is valid, meaning order matters.
So "just use binary search" is only half the story. Real advice needs:
- What questions will I ask and how often?
- Can I afford to build a data structure that enables fast skipping?
- Who keeps the structure in shape as things change?
The answer might be a sorted array and binarySearch. Sometimes it's a hash map for O(1) lookups, which is a different contract—equality, not order. Or, with a small collection, just search the lot.
Back to the shelf
Let's return to the steelbooks.
If the collection is a mess and I need "The Dark Knight" once, I just walk the shelf.
If the collection is already sorted and I'll search often, I'll jump to the middle, halve the remaining candidates, and repeat until I find what I need.
Binary search feels like magic until you see the real secret: the hard work was done beforehand by getting things sorted.
Halving only works if the shelf was already arranged in order.