Lesson 5 of 5 · Open
Searching by halving
Why you do not have to look at everything in a sorted list.
You are looking for the word “planet” in a thick dictionary. You do not start on page 1. You open the book roughly in the middle and land on words that start with L. P comes after L in the alphabet, so your word comes later in the book. You no longer have to look at the whole front half.
This works because the dictionary is sorted: the words are in a fixed order, here alphabetical order. Numbers are often sorted in ascending order, from smallest to largest. In a sorted list, a look at one single entry, that is, one word or one number in the list, tells you on which side of it the thing you want must be.
Look at the middle, throw away half
Computers can search a sorted list in exactly this way. The method is called binary search. “Binary” means made of two parts: every look splits the list into two halves, and the search goes on in only one of them. A step always runs the same way:
Quick check
The middle is not what you want. What do you do next?
Example
Finding 53 among 15 numbers
The list is sorted in ascending order: 2, 5, 9, 14, 18, 26, 29, 35, 42, 46, 53, 57, 61, 68, 75. You are looking for 53.
- In the middle of the 15 numbers is 35. 53 is greater, so everything up to 35 drops out. 7 numbers are left: 42, 46, 53, 57, 61, 68, 75.
- In the middle of the 7 numbers is 57. 53 is less, so everything from 57 on drops out. 3 numbers are left: 42, 46, 53.
- In the middle of the 3 numbers is 46. 53 is greater, so everything up to 46 drops out. 1 number is left.
- The look at this last number shows: it is 53. Found after 4 looks. Going one by one from the front would have taken 11.
How many looks does it take?
In the example, 15 numbers became 7, then 3, then 1. After every look at most half is left, because the middle itself drops out too.
So you can work out in advance how long the search takes at most: halve the length of the list again and again until 1 is left, and keep count. If a half does not come out even, round down. At the end, add the look at the last entry.
Example
One name among 1,000
A list of 1,000 names is in alphabetical order. How many looks do you need at most until you have found a name?
- Halve again and again and round down: after 9 halvings only one name is left.
- Add the look at this last name: at most 10 looks.
- For comparison: going one by one from the front takes about 500 looks on average.
The longer the list, the bigger the advantage. If it becomes twice as long, that costs just one more look: the first look turns the doubled list back into the single one. This is how many looks you need at most:
| Entries | One by one | By halving |
|---|---|---|
| 1,000 | 1,000 | 10 |
| 2,000 | 2,000 | 11 |
| 4,000 | 4,000 | 12 |
| 1 million | 1 million | 20 |
It does not work without sorting
The middle only tells you something about the rest when the list is sorted by exactly what you are looking for. A phone book is sorted by name. If you look for a name in it, you can halve. If you look for a certain number in it, the middle does not help, because the numbers are in no order.
Typical mistake
Looking at the middle of an unsorted list as well and throwing away one half. That cannot be right: without sorting, the middle tells you nothing about the other entries. What you want may just as well be in the half you threw away. Then you have to check entry by entry, or sort the list first.
Remember
Tasks
Task 1 · Choice
A list is sorted in ascending order. You want 23, and the middle entry is 40. What drops out?
Task 2 · Input
A sorted list has 64 entries. How many times do you halve it until only one is left?
Task 3 · Sorting
Where can you search by halving, and where not?
6 cards left
Go deeper
Task 4 · Input · optional
Guess a number from 1 to 100: you always name the middle and hear “higher” or “lower.” How many guesses do you need at most?
Task 5 · Order · optional
Index cards 1 to 31, sorted. You want card 21 and always pull the middle card of what is left. In which order do you pull the cards?
- 1Card 16
- 2Card 20
- 3Card 21
- 4Card 24
- 5Card 22
Task 6 · Pairs · optional