Show contents
Chapter 3 of 4 · 15 min
Comparing two solutions
By the end of this chapter, you'll be able to
- Count the number of steps of an algorithm in the worst case
- Describe one-by-one search and halving search
- Choose between two algorithms depending on the size of the problem
- Explain why halving search requires sorted data
Two ways to search
The game: find a secret number between 1 and 100. After each guess, you're told "higher", "lower" or "found".
Method 1, one by one: try 1, then 2, then 3… If the number is 100, it takes 100 guesses. That's the worst case.
Method 2, halving: always guess the middle of the remaining range. 50? "Higher." That leaves 51 to 100: guess 75. Each answer eliminates half of the possibilities. This is called binary search.
Found in 6 guesses, compared with 73 using the one-by-one method.
Text description of the animation
- The secret number is 73. The search range covers 1 to 100.
- We note the bounds of the range: low = 1, high = 100.
- Guess 1: we try the middle, 50.
- 50 is too low: we only keep 51 to 100. Half is eliminated.
- Guess 2: the middle of 51 to 100 is 75.
- 75 is too high: 51 to 74 remain.
- Guess 3: we try 62, too low. 63 to 74 will remain.
- Guess 4: 68, too low. 69 to 74 will remain.
- Guess 5: 71, too low. 72 to 74 will remain.
- Guess 6: 73.
- Found in 6 guesses, compared with 73 using the one-by-one method.
Counting the steps
With the halving method, each guess cuts the range in two: 100 → 50 → 25 → 13 → 7 → 4 → 2 → 1. In the worst case, 7 guesses are enough for 100 numbers. For a million numbers, it takes at most 20, whereas the one-by-one method could take a million.
The program below counts the guesses of both methods for every secret number and keeps the worst case:
def guesses_one_by_one(secret):
return secret # we try 1, 2, 3… up to the secret
def guesses_halving(secret, low=1, high=100):
guesses = 0
while True:
guesses += 1
guess = (low + high) // 2
if guess == secret:
return guesses
if guess < secret:
low = guess + 1
else:
high = guess - 1
worst_1 = max(guesses_one_by_one(s) for s in range(1, 101))
worst_2 = max(guesses_halving(s) for s in range(1, 101))
print("One by one, worst case:", worst_1)
print("Halving, worst case:", worst_2)
One by one, worst case: 100
Halving, worst case: 7
Your turn
Mini-lab: what about 1,000 numbers?Python 3.13
Output
Click “Run”: Python will load in your browser (about 10 MB the first time).Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Worked exercise
Worked exercise: the right tool
For each situation, say which search method to use and why:
- Finding the word "orange" in a paper dictionary.
- Finding your USB stick in a messy drawer.
Hint 1
Show the solution and common mistakes
- Halving: the dictionary is sorted alphabetically. You open it in the middle, check whether "orange" comes before or after, and eliminate half of the pages each time.
- One by one: the drawer isn't sorted, so opening it "in the middle" tells you nothing. You have to look at each object.
Common mistake: thinking the halving method is always better. It is much faster, but only on sorted data. Sorting has a cost too: for a single search in a small drawer, looking one by one is simpler.
Summary
- 1We compare two algorithms by counting their steps in the worst case.
- 2One-by-one search can take as many steps as there are items.
- 3Halving search eliminates half of the possibilities at each step.
- 4Halving search requires sorted data.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.