Unit 4 · Algorithms & Simulation · Lesson 4.5

Linear vs Binary: The Payoff

Two tables, side by side. A million items: one algorithm needs a million checks, the other needs twenty. Today you put that difference into words — and learn the one situation where the slow one still wins.

Big Idea 3 — Algorithms & ProgrammingCED 3.11 + 3.17 · end of Week 1

Before this lesson

No reading — this page is the reading. Runestone doesn’t cover Unit 4 (search, efficiency, limits, simulation), so everything you need is right here.

1

Where We Are

People→ Bits→ Programs→ Internet→ Security→ Big Data→ Impact

Programs, Algorithms region — end of Week 1. You have two search algorithms and two step-count tables. Today they meet, and 4.1’s missing vocabulary for “better” finally shows up.

2

The Hook

Put both tables on the board. Linear search on 1,000 items: 1,000 checks. Binary search on 1,000 items: 10. Now a million items. Before you look below — guess both numbers.
3

The Idea

List size (n)Linear — worst caseBinary — worst case
10104
1001007
1,0001,00010
1,000,0001,000,00020
The whole week in one line: double the list, and linear search’s worst case doubles. Double the list, and binary search’s worst case goes up by one.

Where does the binary number come from? Count how many times you can halve n before nothing is left. That’s a loop you can write:

Python — count the halvings

n = 1000
steps = 0
while n >= 1:
    n = n // 2
    steps = steps + 1
print(steps)    # 10

AP Pseudocode

n ← 1000
steps ← 0
REPEAT UNTIL (n < 1)
{ n ← (n - (n MOD 2)) / 2
  steps ← steps + 1 }
DISPLAY(steps)

1000 → 500 → 250 → 125 → 62 → 31 → 15 → 7 → 3 → 1 → 0: ten halvings. That matches binary search’s worst case exactly — checked for every n from 1 to 5,000.

The honest caveat: linear search isn’t always wrong. Binary search needs sorted data, and sorting costs more work than a single linear search. Searching an unsorted list once? Just use linear search. Searching the same list thousands of times? Sort it once, then binary search every time.
4

Try It — Race to a Billion

Pick a size, or keep pressing Double it. Watch one number explode and the other barely move.

n =
5

Vocabulary

efficiency Programs
How much work (steps, time, memory) an algorithm uses as its input gets bigger. Compared by counting, not by stopwatch.

That’s the word 4.1 was missing. Algorithm B from 4.1 wasn’t wrong. It was less efficient.

6

Check

Five questions — “count the steps” items join the warm-up rotation starting today.

0 of 5 answered
7

Impact Check

Thirty-three steps to find anyone

There are about 8 billion people on Earth. A sorted list of all of them could be searched in at most 33 checks. Linear search could need 8 billion.

That gap is why giant databases — license plates, faces, phone numbers, purchase histories — are useful at all. Efficient search is what makes it possible to look anyone up in an instant. Whether anyone should be able to is a question the algorithm can’t answer.

8

Connections

Came from 4.3 + 4.4 — your two lab tables, finally side by side.
Next in 4.6 — counting steps for any code, not just searches: one loop vs a loop inside a loop.
Returns in Unit 5 — real datasets are big enough that the search you pick matters.
Returns on the exam — “the list doubles; what happens to the number of steps?” Linear doubles. Binary adds one.
← 4.4 Binary Search