Course 3.1Lesson 2 of 5
Binary search needs a list that is already in order. It looks at the middle item, and throws away the half that cannot hold the target, then repeats on what is left, so each check halves the list. It uses a low and a high position and a while loop that continues while low is not more than high. A million items need at most about 20 checks, against a million for linear search. The price is that the list must be sorted first, and on a list that is not in order, binary search gives wrong answers with no message.
Try the code and the simulations beside the lesson.
After this lesson you can
low, a high and a middle,You played the number guessing game: "Too high", "Too low". A good player does not guess 1, 2, 3. A good player guesses the middle each time, and halves the possibilities. Binary search does exactly that. It needs one thing: the list must be in order.
Think of a dictionary of 1000 pages. To find a word, you do not read from page 1. You open it in the middle, see whether your word comes before or after, and then you open the middle of that half. In ten openings you reach the page.
Predict first. The program looks for 23 in a sorted list of 10 numbers. How many steps will it need? Then run it.
It needed 3 steps. Follow the output with the picture.
low is 0 and high is 9. The middle position is (0 + 9) // 2 = 4, and it holds 16. We want 23. Since 16 is smaller than 23, everything up to position 4 can be thrown away: low = middle + 1.low is 5 and high is 9. The middle is 7, and it holds 56. That is bigger than 23, so everything from position 7 can be thrown away: high = middle - 1.low is 5 and high is 6. The middle is 5, and it holds 23. Found.Notice the three ideas of the loop.
low <= high, which means that there is still something left to look at.low up or high down. It always goes past the middle (middle + 1 or middle - 1), because the middle has just been checked. If you wrote low = middle, the loop could go on for ever.low becomes bigger than high, nothing is left, and the target is not there: the answer is -1.The second call, for 10, shows a failed search: the range closes in until there is nothing left, in 4 steps.
Each step halves the list. So the number of steps is about the number of times that you can halve the list before one item is left:
| Items | Linear search (worst) | Binary search (worst) |
|---|---|---|
| 10 | 10 | 4 |
| 100 | 100 | 7 |
| 1 000 | 1 000 | 10 |
| 1 000 000 | 1 000 000 | 20 |
Look at the last row. A million items, and only 20 checks. If you double the length of the list, binary search needs only one more check. That is the power of the method. The price: the list must be sorted first.
The numbers along the top are some of the items: the list must be in order, or throwing away half could throw away the target.
After sorted(messy), the same function finds it. So the rule is: binary search works only on a sorted list, and it fails quietly if the list is not. Always sort first, or be sure that the data is already in order.
Python has a module for this, bisect, which does binary search for you. It is good to know that it exists. It is also good to know how it works, which is why you write it yourself here.
while low < high instead of <=. The last item is never checked.low = middle instead of middle + 1. An endless loop.(low + high) / 2 is a decimal number, and cannot be a position. Use //.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
low <= high mean?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.