Finding a value by halving
After this: Say why a million sorted items need at most 20 looks.
On this page · 9 sections · 3 checks
Suppose you have a million numbers and you want to find one of them. If they are in no particular order, you may need a million looks. If they are in order, twenty looks are enough. This lesson shows how, starting with a small game.
What sorted means
A list is when its values are in order, usually from smallest to largest. Each value is at least as big as the one before it.
[3, 8, 12, 20]is sorted.[3, 8, 8, 20]is also sorted. Repeated values are allowed.[12, 3, 20, 8]is not sorted, because 3 comes after 12.
A guessing game
I am thinking of a number from 1 to 100. You guess, and I answer "too high", "too low" or "correct".
If you guess 1, then 2, then 3, you could need 100 guesses, because each "too low" rules out only one number.
A better first guess is 50, the middle. If I say "too high", then 50 and every number above it are ruled out. One answer removes half of the numbers, because they are in order.
Here is a full game. My number is 40. Each time, guess the middle of the numbers still possible. If the middle falls between two numbers, take the lower one.
- Guess 50: too high, so the number is from 1 to 49.
- Guess 25: too low, so it is from 26 to 49.
- Guess 37: too low, so it is from 38 to 49.
- Guess 43: too high, so it is from 38 to 42.
- Guess 40: correct.
That took five guesses, and seven are enough for any number from 1 to 100. A printed phone book, with its names in alphabetical order, works the same way: a 1,000-page book takes at most ten openings.
The same idea on an array
An array is a row of boxes called cells. Each cell has a position number called its index, starting at 0. What an array is explains these words.
Here is a sorted array of five cells. The value you are looking for is called the target, and here it is 40.
| Index | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| Value | 10 | 20 | 30 | 40 | 50 |
- Look at the middle cell, index 2. It holds 30, which is too small. The array is sorted, so the cells before it are too small as well, and we ignore indexes 0 to 2.
- Two cells are left, and we take the left one as the middle. Index 3 holds 40, so we have found it.
This method is called . Look at the middle of a sorted row, decide which side the target must be on, ignore the other side, and repeat until you find the target or no cells are left.
One look at a middle cell is called a . This search took two probes.
Why the row must be sorted
On an unsorted row, one look tells you about one cell only. Take [30, 10, 50, 20, 40] and search for 40. The middle cell holds 50, which is too big, so the method ignores everything to the right. But 40 is on the right, at index 4, so the search would miss it.
How many probes does it take?
Each probe rules out about half of the cells. So the question is: how many times can you cut this number in half? Try it with 8 cells.
- Cut 8 in half, and 4 are left.
- Cut 4 in half, and 2 are left.
- Cut 2 in half, and 1 is left.
That last cell still needs one look, so 8 cells take at most 4 probes.
Programmers call the number of items n. The number of times you can cut n in half before you reach 1 is written , said "log base 2 of n". So log₂ 8 is 3.
Most numbers do not halve evenly, so we drop the remainder: 10 goes to 5, 2 and 1, which is three cuts. Ten 2s multiplied together make 1,024, so log₂ 1,000 is a little under 10.
| Cells | Cuts to reach one cell | Most probes needed |
|---|---|---|
| 10 | 3 | 4 |
| 1,000 | 9 | 10 |
| 1,000,000 | 19 | 20 |
| 1,000,000,000 | 29 | 30 |
So binary search takes about log₂ n probes, and a thousand times more cells adds only ten more. This short program does the cutting for us.
def probes_needed(n): count = 0 while n > 0: # each probe leaves at most half of the cells n //= 2 count += 1 return count
for n in [10, 1000, 1_000_000]: print(n, probes_needed(n))10 4 1000 10 1000000 20
static int probesNeeded(int n) { int count = 0; while (n > 0) { // each probe leaves at most half of the cells n /= 2; count++; } return count;}
for (int n : new int[] {10, 1000, 1_000_000}) System.out.println(n + " " + probesNeeded(n));10 4 1000 10 1000000 20
- n //= 2n /= 2 cuts n in half and drops any remainder.
- The loop stops at 0, not 1. Its last pass stands for the final look at the one remaining cell, so
countends up as the number of probes in the table.
Scan or halve?
Without sorted order, you need a : check the cells one at a time from the start. That is O(n), which means the work grows at the same rate as the data. Binary search is O(log n), which means that when the data doubles, you need about one more probe. Counting steps explains this notation.
Watch it happen
Run a linear scan for 23 and for 91, and count the comparisons. Then run binary search for the same two values, and count the probes.
- Search for 23A linear scan from the start. Count the comparisons before 23 turns up.
- Search for 91
- Binary search for 23
- Binary search for 91
- 02
- 15
- 28
- 312
- 416
- 523
- 638
- 756
- 872
- 991
Run an operation to begin.
Check yourself
A sorted array holds 1,000 numbers. At most how many probes does binary search need to find a value, or to be sure it is not there?
Why does binary search only work on a sorted array?
What are the three counts this prints, for 10, 1,000 and 1,000,000?
def probes_needed(n): count = 0 while n > 0: # each probe leaves at most half of the cells n //= 2 count += 1 return count
for n in [10, 1000, 1_000_000]: print(n, probes_needed(n))static int probesNeeded(int n) { int count = 0; while (n > 0) { // each probe leaves at most half of the cells n /= 2; count++; } return count;}
for (int n : new int[] {10, 1000, 1_000_000}) System.out.println(n + " " + probesNeeded(n));Connect
- Say what sorted means, and explain that one probe of a sorted row rules out about half of what is left.
- Say that a thousand items need at most 10 probes and a million need 20, because the count is about log₂ n, the number of times you can cut n in half.
- Say why binary search needs the row to be sorted.