Skip to content
KALANBLON
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.

Step 11 of 11
Text description of the animation
  1. The secret number is 73. The search range covers 1 to 100.
  2. We note the bounds of the range: low = 1, high = 100.
  3. Guess 1: we try the middle, 50.
  4. 50 is too low: we only keep 51 to 100. Half is eliminated.
  5. Guess 2: the middle of 51 to 100 is 75.
  6. 75 is too high: 51 to 74 remain.
  7. Guess 3: we try 62, too low. 63 to 74 will remain.
  8. Guess 4: 68, too low. 69 to 74 will remain.
  9. Guess 5: 71, too low. 72 to 74 will remain.
  10. Guess 6: 73.
  11. 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:

  1. Finding the word "orange" in a paper dictionary.
  2. Finding your USB stick in a messy drawer.
Hint 1
Ask yourself whether the items are arranged in an order that lets you say "further on" or "before".
Show the solution and common mistakes
  1. 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.
  2. 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

  1. 1We compare two algorithms by counting their steps in the worst case.
  2. 2One-by-one search can take as many steps as there are items.
  3. 3Halving search eliminates half of the possibilities at each step.
  4. 4Halving search requires sorted data.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.