Three ways to find a word in a dictionary. All correct, all algorithms, only one fast. Today: what makes something an algorithm — and the first hint that correct isn’t the same as good.
This is the only Runestone reading for Unit 4. The book doesn’t cover search, efficiency, or simulation — from 4.2 on, these lesson pages are the reading. Enrolled in our Runestone course? Open it from there so your progress counts.
A new unit, but you haven’t left Programs. Unit 4 builds the Algorithms region of the map — the bridge between the programs you write and the bits they run on. No new syntax this unit. Every tool is from Unit 3; you’re about to find out which uses of those tools are good.
1. Start on page 1 and read every word until you hit it.
2. Flip to the Q section, then go page by page.
3. Open to the middle. Is “quokka” before or after this page? Throw away the wrong half. Repeat.
All three are correct. All three are algorithms. Only one of them is fast — and by the end of this week you’ll be able to say exactly how much faster, with a number you produced yourself.
An algorithm is a finite set of instructions that accomplishes a specific task. That’s it — no computer required. A recipe is an algorithm. So are directions to a friend’s house. And every algorithm you will ever write is built from just three pieces, the same three you’ve been using since Unit 2:
| Building block | What it means | You first met it |
|---|---|---|
| sequencing | Steps run in order, one after another | 2.1 — your first program |
| selection | A condition decides which steps run | 2.12 — if / else |
| iteration | Steps repeat | 3.1 — the while loop |
def biggest_b(nums): for x in nums: is_max = True for y in nums: if y > x: is_max = False if is_max: return x
PROCEDURE biggestB(nums) { FOR EACH x IN nums { isMax ← true FOR EACH y IN nums { IF (y > x) { isMax ← false } } IF (isMax) { RETURN (x) } } }
Hover or tap a line to light its twin. Find all three building blocks: the lines run
in order (sequencing), the IFs choose (selection), the FOR EACHes repeat
(iteration). Algorithm B is correct. The question is whether it’s good.
Algorithm A is your 3.9 max: one pass, keep the biggest. Algorithm B is the challenger above. Both
count every > comparison they make. Change the list and watch the two numbers.
Try the 20-item preset. Both algorithms agree on the answer — so which is “better”? Notice you can say B did more comparisons, but not yet how much worse B gets as the list grows. That missing vocabulary is what this week is for.
All four are review. Today they become the definition of the thing this whole unit is about.
Five questions. One is a REPEAT UNTIL trace — the flips don’t take a holiday.
An organ-donor matching system has to compare every patient on a waiting list against every new donor. Two algorithms both produce correct matches. One finishes in seconds. The other finishes in days — and a donated organ lasts hours.
When an algorithm runs on real people’s time, “it gets the right answer eventually” can still be the wrong algorithm. That’s why the rest of this unit measures how much work, not just whether it works.