← Back to course
Code Crew G5 / Week 07 / Thursday
4/6
Week 07 Β· Binary search

Thursday

Double the list, add one check
// Cut it in half
⏱ about 15 min

Thursday: Double the list, add one check

The catalog keeps growing: 8 stars, then 16, then 32, then 64.

"Linear search gets twice as slow each time it doubles," Rocket says. "What about binary search?"

Raven makes a table on her wrist computer. "What do you notice when we count the worst case for each size?"

The numbers appear: 4, 5, 6, 7. Rocket gasps. "It only goes up by one!"

Nova sparkles. "Here is a hint. Think about what one extra check does to a list."

Counting the worst case

This program counts checks for every possible target, including missing ones. Then it keeps the biggest count. max() gives the bigger of two values.

def checks_needed(items, target):
    low = 0
    high = len(items) - 1
    checks = 0
    while low <= high:
        mid = (low + high) // 2
        checks = checks + 1
        if items[mid] == target:
            return checks
        elif items[mid] < target:
            low = mid + 1
        else:
            high = mid - 1
    return checks

for size in [8, 16, 32, 64]:
    items = list(range(size))
    worst = 0
    for target in range(-1, size + 1):
        worst = max(worst, checks_needed(items, target))
    print(size, worst)
Output:
8 4
16 5
32 6
64 7

Each check cuts the list in half. So one extra check can handle a list twice as big.

That is why doubling the list adds only one check in the worst case.

ItemsLinear search worst caseBinary search worst case
884
16165
32326
64647
Following the pattern, what is the worst case for binary search on 128 items?
WHY THIS EXERCISEOne more halving step handles twice as many items.
USE THE PATTERN.
  • Read the question.
  • Tap your answer.
Binary search worst case for 1,000 items is 10 checks. What about 2,000 items?
Linear search worst case for 64 items?
StatementTrue or false?
Doubling the list doubles the binary search worst case.?
Doubling the list doubles the linear search worst case.?
For 64 items, binary search needs at most 7 checks.?
WHY THIS EXERCISEHalving makes binary search grow very slowly as lists get bigger.

Practice lab: count the checks

def count_checks(items, target):
    low = 0
    high = len(items) - 1
    checks = 0
    while low <= high:
        mid = (low + high) // 2
        checks = checks + 1
        if items[mid] == target:
            return checks
        elif items[mid] < target:
            low = mid + 1
        else:
            high = mid - 1
    return checks

items = list(range(16))
print(count_checks(items, 7))
print(count_checks(items, 0))
print(count_checks(items, 15))
What does the first print line show?
What does the second print line show?
What does the third print line show?
Use the pattern. What is the worst case for 256 items?
PATTERN CHECK
  • Read the question.
  • Tap your answer.
What is the binary search worst case for 512 items?
What is the linear search worst case for 256 items?
What is the binary search best case?
StatementTrue or false?
count_checks(items, 7) finds 7 on the first check.?
Binary search on 16 items can need 16 checks.?
Different targets can take different numbers of checks.?
Linear search on 16 items can need 16 checks.?
WHY THIS EXERCISEThe worst case for binary search grows by one each time the list doubles.
What is the binary search worst case for 32 items?
WHY THIS EXERCISE16 items needs 5, and doubling adds one.

Amazing! You measured how two algorithms grow. Tomorrow we find the one thing binary search must have.

← Wednesday