Binary Search
How binary search finds a value in a sorted list by looking at the middle and throwing away half each time, how to count its looks, and why the list has to be sorted.
1The idea
Think of the number guessing game: “I'm thinking of a number from 1 to 100.” The smart first guess is 50. If the answer is “higher,” you never need to think about 1 to 50 again. Each guess cuts the possibilities in half.
Binary search does this with a list. It starts at the middle of a sorted data set, compares that element with the value it wants, and eliminates half of the data. It repeats on the half that is left until it finds the value or has eliminated every element.
2Looking for 37
The list below is sorted from smallest to largest. When the part being searched has an even number of elements there is no single middle, so these examples use the one just before the center (the middle index rounded down).
| Look | Part still searched (indexes) | Middle index | Middle value | Decision |
|---|---|---|---|---|
| 1 | 1 to 15 | 8 | 30 | 37 is larger: drop the left part |
| 2 | 9 to 15 | 12 | 48 | 37 is smaller: drop the right part |
| 3 | 9 to 11 | 10 | 37 | found at index 10 |
Three looks. The decision at each step depends on one fact: in a sorted list, everything to the left of the middle is smaller and everything to the right is larger. That is why one comparison can rule out half the list at once.
3When the value is not there
Search the same list for 40:
| Look | Part still searched | Middle value | Decision |
|---|---|---|---|
| 1 | 1 to 15 | 30 | 40 is larger: drop the left part |
| 2 | 9 to 15 | 48 | 40 is smaller: drop the right part |
| 3 | 9 to 11 | 37 | 40 is larger: drop the left part |
| 4 | 11 to 11 | 41 | 40 is smaller: drop the right part |
After look 4 nothing is left to search: 40 would have to sit between 37 and 41, and there is no element there. The search reports that 40 is not in the list after 4 looks, without checking the other 11 elements.
4Counting looks
Each look halves the part that is left, so the most looks a search can need is the number of times you can halve the list size before reaching 1, plus one for the last look. Doubling the list adds only one more look.
| Elements in the sorted list | Binary search: most looks | Linear search: most looks |
|---|---|---|
| 15 | 4 | 15 |
| 100 | 7 | 100 |
| 1,000 | 10 | 1,000 |
| 1,000,000 | 20 | 1,000,000 |
To estimate by hand, halve and count: 100 → 50 → 25 → 12 → 6 → 3 → 1 is 6 halvings, so at most 7 looks. For the lists of 15, 100, and 1,000 elements, a program confirmed the binary search numbers by searching for every value in the list and for every missing value in between; the million-element row uses the same halving count.
When a question gives you the list and the target, don't estimate: simulate the search look by look, as in the tables above, and count every look including the one that finds the value.
5Why the list must be sorted
Data must be in sorted order to use binary search. The rule “the target is bigger than the middle, so it must be to the right” is only true when the list is in order. Here is what happens on an unsorted list, searching for 8:
The search reasons “15 is bigger than 8, so 8 must be to the left,” and it is right by luck. Then “33 is bigger, so 8 must be to the left of 33”: wrong, because 8 is to its right. The run:
| Look | Part still searched | Middle value | Decision |
|---|---|---|---|
| 1 | 1 to 7 | 15 | 8 is smaller: keep the left part |
| 2 | 1 to 3 | 33 | 8 is smaller: keep the left part |
| 3 | 1 to 1 | 40 | 8 is smaller: keep the left part |
Binary search on unsorted data does not crash and does not warn you. It reports “not found” for a value that is right there. If the data is not sorted, sort it first or use a linear search.
6Binary or linear?
On sorted data, binary search is often much more efficient than linear search: finding 37 took 3 looks instead of 10, and for a million elements the difference is 20 looks against a million. Linear search still has its place:
| Situation | Better choice | Why |
|---|---|---|
| Large list, already sorted, searched often | Binary search | Few looks each time |
| Unsorted list, searched once | Linear search | Binary search would need the list sorted first, which takes longer than one linear search |
| Very short list | Either | With 5 elements the difference is a look or two |
| Value is first in the list | Linear search | It finds it on the first check |
Binary search starts at the middle of a sorted list and eliminates half of what is left with each look, until the value is found or nothing is left. It needs sorted data, and on sorted data it is often far faster than linear search: doubling the list adds only one look.
7Worked examples
Two targets, one list
Binary search is used on the sorted list 4, 9, 13, 17, 22, 28, 31, 36, 40, 45, 52 (middle rounded down). How many elements are examined to find 45? To find 4?
- 145: look 1 checks 28; look 2 checks 40; look 3 checks 45.
- 24: look 1 checks 28; look 2 checks 13; look 3 checks 4.
- 3A linear search would need 10 checks for 45 but only 1 for 4.
500 sorted values
What is the greatest number of looks a binary search can need on a sorted list of 500 elements?
- 1Halve and count: 500 → 250 → 125 → 62 → 31 → 15 → 7 → 3 → 1.
- 2That is 8 halvings, plus the final look.
The class roster
A teacher's attendance list is in the order students arrived. Can a binary search find a student's name in it? What should the teacher do?
- 1Binary search needs the data in sorted order, here alphabetical order.
- 2Arrival order is not sorted, so the halving reasoning fails and the search can miss names that are there.
A school directory
A district directory has 30,000 names in alphabetical order. About how many looks does a binary search need at most? A linear search?
- 1214 = 16,384 is less than 30,000, and 215 = 32,768 is more.
- 2So the list can be halved 14 times before reaching 1, and one more look finishes: 15.
8Common mistakes
After the first look, the middle is the middle of the part still being searched, not of the original list.
Finding 37 took three looks: 30, 48, and 37. The final comparison counts.
It gives wrong answers without any error. The sorted requirement is not optional.
On an unsorted list that will be searched once, sorting first costs more than one linear search.
“Binary search compares the target with the middle element of the sorted list. If it is not a match, the comparison shows which half the target must be in, so the other half is eliminated. This repeats on the remaining half until the target is found or no elements are left. It requires the list to be sorted.”
Name the middle, the elimination of half, how it ends, and the sorted requirement.