← Back to course
Code Crew G5 / Week 07 / Friday
5/6
Week 07 Β· Binary search

Friday

Sorted or bust
// Cut it in half
⏱ about 15 min

Friday: Sorted or bust

A new batch of star IDs arrives. Rocket drops them straight into binary search and looks for 36.

The answer comes back -1. "But 36 is right there!" Rocket says. "My search is broken!"

Raven reads the list. "What do you notice about the order of these numbers?"

"They are all jumbled up," says Rocket. "30, 3, 21, 8."

Nova hovers over the list. "Here is a hint. Binary search trusts the order. What if the order is wrong?"

Binary search needs a sorted list

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

jumbled = [30, 3, 21, 8, 36, 12, 27, 15]
print(binary_search(jumbled, 36))
Output:
-1

The middle item is 8. 36 is bigger, so the search throws away the left half. Then it checks 12, then 27, then 15.

It never looks at spot 4, where 36 sits. On a jumbled list, the thrown away half might hold the target.

StatementTrue or false?
Binary search works on any list.?
Linear search works on a jumbled list.?
Binary search throws away half the list after each check.?
On a sorted list of 64 items, binary search can take 64 checks.?
WHY THIS EXERCISEBinary search is fast because it trusts the order. A jumbled list breaks that trust.
WHICH SEARCH FITS?
  • Read the question.
  • Tap your answer.
A jumbled list of 10 star names.
A sorted list of 50,000 star IDs.
A sorted catalog has 64 stars. How many checks does binary search need at most?
WHY THIS EXERCISEEach check halves the list. 64, 32, 16, 8, 4, 2, 1 takes 7 checks.

Practice lab: jumbled or sorted

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

jumbled = [9, 2, 7, 4, 5]
in_order = [2, 4, 5, 7, 9]
print(binary_search(jumbled, 2))
print(binary_search(in_order, 2))
print(2 in jumbled)
What does the first print line show?
What does the second print line show?
What does the third print line show?
What item does the search check first in jumbled?
SORTED CHECK
  • Read the question.
  • Tap your answer.
In jumbled, the search checks 7, then 9. Why does it miss 2?
Which search always finds 2 in jumbled?
Is [1, 3, 3, 8] sorted?
StatementTrue or false?
in_order is sorted from smallest to biggest.?
jumbled is sorted.?
Binary search found 2 in in_order.?
in works on a jumbled list.?
WHY THIS EXERCISEBinary search needs sorted data. Linear search and in do not.
A sorted catalog has 128 stars. How many checks does binary search need at most?
WHY THIS EXERCISEDoubling the list adds one check.

Week 7 complete! You found a search that is fast on big sorted lists. But how do lists get sorted? Next week we sort by hand.

← Thursday