How binary search works
How halving a sorted list again and again finds any item in a handful of steps, and why the list has to be sorted first.
Written by Amili, an AI writer, from the sources listed below · 7 October 2026 · 6 min read
Binary search finds a target value in a sorted list by checking the middle item, discarding the half that cannot contain the target, and repeating. Because each step halves what is left, even a huge list needs only a few dozen comparisons, which makes it one of the most widely used search methods.
In short
- Binary search only works on data that is already sorted.
- Each comparison rules out half of the remaining items, so the work grows logarithmically with the size of the list.
- In the worst case it needs about log2(n) + 1 comparisons; a million items take around 20.
- Among methods that work only by comparing items, nothing beats it on average or worst-case performance.
- Small details, such as how the middle point is chosen and how duplicates are handled, are where implementations go wrong.
How does binary search work, step by step?
Start with a list sorted from smallest to largest and a value you want to find. Keep two markers: one at the left end of the part still in play and one at the right end. At first they span the whole list.
Look at the item halfway between the markers. If it equals the target, you are done and can report its position. If the middle item is smaller than the target, the target can only be to its right, so move the left marker just past the middle. If the middle item is larger, the target can only be to its left, so move the right marker just before the middle.
Repeat with the smaller range. Each round throws away the half where the target cannot be. If the markers cross, so that the range is empty, the target is not in the list. Other names for the method include binary chop, logarithmic search and half-interval search.
What does a worked example look like?
Take a sorted list of 15 numbers and suppose you are looking for 42. Check the eighth number, the one in the middle. Say it is 30. Because 42 is larger, everything from the first to the eighth number can be ignored; seven numbers remain.
Check the middle of those seven. Say it is 50. Now 42 must be among the three numbers just before it. Check the middle of those three. If it is 42, the search ends after three comparisons. If not, one more comparison settles it either way.
A plain left-to-right scan might have needed all 15 checks. The gap widens fast as lists grow: doubling the size of the list adds only one extra step to binary search, while it doubles the work of a straight scan.
Why is it so fast?
Because every comparison halves the remaining range, the number of steps grows with the logarithm of the list size rather than the size itself. In the worst case binary search makes about log2(n) + 1 comparisons, rounded down, for a list of n items. In the best case, when the target happens to sit in the middle, a single comparison is enough.
One useful way to picture this is as a tree. The middle item is the root, the middle of the lower half is its left branch, the middle of the upper half is its right branch, and so on. A search walks down from the root, and the depth of the tree caps the number of steps. Because binary search keeps the two halves as even as possible, its tree is as shallow as a tree can be.
That is why no search method that works only by comparing items can beat binary search on average or in the worst case. It also needs very little memory: just a few markers, whatever the size of the list.
Where is binary search used?
Beyond finding exact matches, binary search answers related questions about sorted data. It can count how many items are smaller than a value, find the next-smallest or next-largest item even when the target is absent, find the nearest neighbour, and count the items that fall between two values using two such counts.
Several variants extend the idea. Exponential search adapts it to lists with no known end. Fractional cascading speeds up searching for the same value across several sorted lists, and is useful in computational geometry. Two common data structures for storing and searching data, the binary search tree and the B-tree, are built on the same principle.
Where does it fail or go wrong?
The first limit is the requirement that data be sorted. If the list is not in order, binary search gives wrong answers, and sorting the data first takes time of its own. For very small lists, a simple scan is often just as fast. And some structures built specifically for lookups, such as hash tables, can find exact matches faster still, although they cannot answer the nearest or next-largest questions binary search handles easily.
The second problem is detail. The idea fits in a sentence, but the code has several easy-to-miss decisions. Choosing whether to round the middle point up or down changes which of several equal items is returned. When a value appears more than once, a standard version may return any matching position, so separate versions exist to find the leftmost or rightmost match. Some implementations skip the equality check on each round and test only when one item is left; that makes each round cheaper but adds about one extra round on average. Getting these edges right matters more than the core idea.
What does it teach about thinking?
Binary search shows the value of questions that split the possibilities in two. A guess that rules out half of the options is worth far more than one that rules out a single option, and organising information well in advance, here by sorting, is what makes those powerful questions possible.
It also shows that a simple idea still demands care. Most of the effort in a correct binary search goes into the edges: empty ranges, repeated values and the exact moment to stop.
Questions people ask
Why does binary search need a sorted list?
Binary search decides which half to throw away by comparing the target with the middle item. That decision is only valid if every item on one side is smaller and every item on the other side is larger. In an unsorted list, the target could be on either side, so discarding a half could throw it away and the search would wrongly report that it is missing.
How many steps does binary search take?
In the worst case, about log2(n) + 1 comparisons for a list of n items, rounded down. That means roughly 10 for a thousand items, 20 for a million and 30 for a billion. In the best case, when the target is exactly in the middle, it finishes after one comparison. On average it takes close to the worst case, since most items sit deep in the search tree.
Is binary search faster than a hash table?
For finding an exact match, a hash table can be faster, because it jumps almost directly to where an item should be. Binary search has other strengths: it works on any sorted list without extra structure, uses very little memory and can answer questions a hash table cannot, such as finding the next-largest value or counting items between two values.
The thinking behind it
It introduces binary search early, alongside other beginner algorithms such as sorting and graph search.
Read or listen to Grokking Algorithms
Hear the whole book free: start an Audible trial and your first audiobook — this one, if you like — is on the house.
As an Amazon Associate, ReadGlobe earns from qualifying purchases and Audible trials — at no extra cost to you.
Sources
- Binary search — Wikipedia
- Search algorithm — Wikipedia
How this was made: Amili, an AI writer, wrote this article in its own words from the sources above. Every link was checked before publishing. Spotted an error? Tell us and we will correct it.