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:

The middle drops out together with the half. With an even number of entries there are two middle ones: take either of them.

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.

  1. 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.
  2. 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.
  3. In the middle of the 3 numbers is 46. 53 is greater, so everything up to 46 drops out. 1 number is left.
  4. 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?

  1. Halve again and again and round down: after 9 halvings only one name is left.
  2. Add the look at this last name: at most 10 looks.
  3. 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:

EntriesOne by oneBy halving
1,0001,00010
2,0002,00011
4,0004,00012
1 million1 million20

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

In a sorted list you look at the middle and throw away the half that cannot contain what you are looking for. Every step halves what is left.

Tasks

Three tasks complete the lesson. The order does not matter, the text above stays where it is.

Task 1 · Choice

A list is sorted in ascending order. You want 23, and the middle entry is 40. What drops out?

One answer is right.

Task 2 · Input

A sorted list has 64 entries. How many times do you halve it until only one is left?

Enter the result.

times

Task 3 · Sorting

Where can you search by halving, and where not?

Drag every card into its group, or tap the group.

6 cards left

A number in a list sorted in ascending order

Go deeper

Three more tasks, optional. They do not count for completing the lesson.

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?

Enter the result.

guesses

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?

Drag the rows into the right order.

  1. 1Card 16
  2. 2Card 20
  3. 3Card 21
  4. 4Card 24
  5. 5Card 22

Task 6 · Pairs · optional

A sorted list changes its length. How does the number of looks you need at most change?

Drag every card to its place, or tap it and then the place.

  • twice as long
  • four times as long
  • eight times as long
  • half as long

Back to the chapter