Imagine I'm thinking of a number between
That is binary search: a way of finding something in a sorted list by repeatedly looking at the middle item and throwing away the half that can't possibly contain what you're after. "Binary" means "two", and it's the perfect name — each step splits the list in two and keeps just one half.
You already do this without thinking. To find "otter" in a dictionary you don't read from "aardvark" — you flop it open near the middle, land on "lemur", and instantly ignore the entire first half. The words being in order is what makes that leap safe, and it's exactly the secret behind binary search.
We track the slice of the list still worth searching with two markers: low (the first index
that might hold the target) and high (the last one). The item bang in the middle is at index
mid, which is just the average of the two, rounded down:
Each round, we compare the middle item with the target and do one of three things:
low to mid + 1;high to mid - 1.
We repeat while low is still at or before high. If the two markers ever cross
over — low greater than high — the slice is empty, which means the target simply
isn't in the list.
Here is a sorted list of low…high
window collapse: every step at least halves the shaded region still in play.
Notice we found
Here is the whole algorithm in TypeScript. It console.logs the window and the item it checks
on every round, so you can see the halving happen. Press Run:
Read the log for [0..10] to [0..4] to [3..4], closing in with each line. Searching
for low eventually overtakes
high, the loop ends, and we return
Its rival,
The gap is staggering as the list grows. For a list of
Because binary search demands a sorted list, and sorting isn't free — it costs more work than a single linear scan. So if you're going to search a list just once, plain linear search is often the sensible choice. Binary search wins big when the list is already sorted, or when you'll search it many times: you pay to sort once, then every lookup afterwards is lightning-fast. Databases lean on exactly this trade — they keep data in sorted "indexes" so that countless later searches each cost only a handful of steps.
Binary search only works on a sorted list. The whole method rests on one promise: if
the target is bigger than the middle item, it must be to the right; if smaller, to the left.
On an unsorted list that promise is broken, so throwing away a half is a reckless
gamble — the target might be sitting in the half you just discarded. It won't crash; it will calmly
return the wrong answer, usually
The fix is simple: sort first, then binary-search. If you can't sort (or the data keeps changing), reach for linear search instead — it never assumes any order, so it always gives the right answer, just more slowly.