Lite / Discovering How the World Works / Week 20
Some algorithms are better than others
Two robots count 30 boxes. One counts in ones, the other in twos. Both get 30. Which would you hire?
Why should I care?
Once an algorithm works, engineers ask: can it be faster, cheaper, safer? A search engine, a sat-nav and a sorting machine all depend on clever algorithms that do the same job in far fewer steps. Counting steps is the first tool for judging an algorithm.
- Compare two correct algorithms by counting their steps.
- Explain why fewer steps matters for machines.
- Spot when a faster algorithm stops being correct.
Predict
Counting 30 people one at a time takes 30 counts. How many counts does it take if you count them in pairs (2, 4, 6…)?
- 30
- 15
- 60
Guess first, then reveal
15 counts. Same answer, half the work. And if you ever have an odd number, you need one extra careful step at the end, or your clever algorithm gives the wrong answer.
Discover
Choose 'Repeat until', start x at 0 and stop at 30. Run it adding 1 each pass, then adding 2, then 3. Write down how many passes each takes.
What the interactive shows (text version)
The loop tracer can be replaced by this table for 'repeat until x ≥ 30, starting at 0': adding 1 each pass takes 30 passes; adding 2 takes 15; adding 3 takes 10; adding 4 takes 8 passes (x reaches 32, which overshoots 30).
Watch (optional)
Watch for: Watch for two ways of sorting the same list and how different their amount of work is.
Shows how a step-by-step recipe of decisions and repeats lets a machine solve a problem such as sorting.
“Intro to Algorithms: Crash Course Computer Science #13” by CrashCourse (opens YouTube; streaming video uses a lot of data)
The video is optional. Everything you need is in this lesson's text.
Understand
Correct first, then fast
An algorithm that gives the wrong answer quickly is worse than useless. So engineers first check that an algorithm is correct for every case, then ask how many steps it takes.
Counting steps
We judge an algorithm by how many steps it needs, especially as the job gets bigger. Counting by ones takes 30 steps for 30 people and 1,000 steps for 1,000. Counting by twos takes half as many either way.
Clever tricks need checking
Faster algorithms often take a shortcut, and shortcuts have edge cases. Counting in twos goes wrong with an odd number of people unless you handle the last one. Always test the awkward cases: zero, one, odd, the biggest.
Explain it another way
Intuitive
Looking for a word in a dictionary, you don't start at page 1: you open it in the middle and jump forwards or backwards. Same answer, far fewer pages turned.
Technical
Engineers describe how an algorithm's step count grows with the size of the problem. Counting one at a time grows in step with n; repeatedly halving (like the dictionary trick) grows much more slowly.
Mathematical
Counting n items in groups of g takes ⌈n ÷ g⌉ counts. For n = 30: g = 1 gives 30, g = 2 gives 15, g = 3 gives 10, g = 4 gives ⌈7.5⌉ = 8, because the last group is partly full.
Applied
A warehouse robot that plans its route with a smarter algorithm can pick orders in a fraction of the time. Over millions of orders, that saves enormous amounts of energy and money.
Worked example 1
A robot must check 20 lockers. Algorithm A checks every locker. Algorithm B checks only even-numbered lockers. Which is better?
It depends on what is correct! If the job is 'find the locker with the parcel', B is twice as fast but wrong half the time: it never looks in odd lockers. A takes 20 steps but always works. Correctness comes before speed.
Common mix-ups
The fastest algorithm is always the best.
✓ The best algorithm is correct first, then as efficient as it needs to be.If it works for one example, it works.
✓ Test the awkward cases too: zero, one, odd numbers and very large inputs.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
Apply it
Invent two algorithms for finding the tallest person in your class. Count how many comparisons each needs for 10 people. Is one always correct? Is one faster?
Reflect
Can you think of a shortcut you use that sometimes gives the wrong answer?
Write your answer on paper or in your own notes. Lite never stores what you write.