← Back to course
2/6
Week 07 · Search, Speed and Limits

Tuesday

Binary search, step by step
// Binary search, counting steps, heuristics and problems no algorithm can solve
⏱ about 20 min

Tuesday: Binary Search, Step by Step

Comet copies fifteen of Beacon's sorted water readings onto cards and lines them up along the bench.

"Fifteen cards," she says. "Linear search could take fifteen checks. How many does binary search take?"

Wren picks up a pencil. "Let's investigate. We will count every check."

Nova projects a small number above each card: 1, 2, 3, all the way to 15. "These are the indexes," she says.

"The list starts at index 1," Wren reminds them, "just like in Beacon's pseudocode."

Comet taps card 8. "This one is in the middle. Let's go."

The sorted log

Here are the fifteen readings as a Beacon list. The values are sorted from smallest to largest.

waterLog ← [12, 18, 23, 31, 37, 42, 48, 55, 61, 66, 70, 74, 81, 88, 95]

index:      1   2   3   4   5   6   7   8   9  10  11  12  13  14  15

Binary search keeps track of two indexes. Low is the first index still in play. High is the last.

The middle index is low plus high, divided by 2. When that falls between two indexes, the crew rounds down.

At the start, low is 1 and high is 15. So the first middle index is 8.

Trace: searching for 74

CheckLowHighMiddle indexValue thereWhat happens
111585555 is less than 74, so keep indexes 9 to 15
29151274Found it

Binary search found 74 in 2 checks. Linear search would have checked indexes 1 through 12: 12 checks.

TRACE: SEARCHING FOR 23
  • Read the question.
  • Tap your answer.
Check 1 looks at index 8. What value is there?
23 is less than 55. Which indexes are still in play after check 1?
Low is 1 and high is 7. What is the middle index for check 2?
Index 4 holds 31, then index 2 holds 18, then index 3 holds 23. How many checks in all?
Linear search for 23 starts at index 1. How many checks does it need?

Binary search is often more efficient, not always. A value near the start of a list can be quicker to find by linear search.

Counting steps measures efficiency

Efficiency is an estimate of how much computing an algorithm uses. It is usually written in terms of the size of the input.

You can measure efficiency informally by counting how many times a statement, or a group of statements, runs.

Different correct algorithms for the same problem can have different efficiencies. Both searches are correct, but they are not equally fast.

The table shows the most checks each search could need, using the crew's round-down rule.

Sorted list sizeLinear search, most checksBinary search, most checks
884
16165
32326
64647
1281288
READ THE EFFICIENCY TABLE
  • Read the question.
  • Tap your answer.
A sorted list has 64 readings. What is the most checks binary search could need?
When the list size doubles, what happens to binary search's most checks?
Following the pattern, a sorted list of 256 readings needs at most how many binary search checks?
Binary search needs the data to be ____ first.
One informal way to measure efficiency is to ____ how many times statements run.
StatementTrue or false?
Binary search always takes fewer checks than linear search.?
Two correct algorithms for the same problem can have different efficiencies.?
For linear search, the most checks grows at the same rate as the list.?
On the AP reference sheet, the first index of a list is 0.?
WHY THIS EXERCISEEfficiency is about how steps grow, and "often faster" is not the same as "always faster."
On paper, make your own trace table for a search for 95 in waterLog. Use the columns check, low, high, middle index and value there.
CHECK YOUR TRACE FOR 95
  • Read the question.
  • Tap your answer.
Your checks for 95 look at indexes 8, 12 and 14 first. Which index comes next?
How many checks does binary search need to find 95?

Nice tracing. Tomorrow's Beacon Lab turns binary search into a card game.

← Monday