Skip to content
Tactiverge

School/◇ Explorer/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?

About 25 min · 3 goals · Checked by automated tests; not yet reviewed by an educator

1.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.

2.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…)?

Guess first. Being wrong here is how the surprise works.

3.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.

Loop tracer

A loop repeats the same instructions. Step through one pass at a time and fill in the trace table, the way engineers check a loop by hand.

Kind of loop
0
3
4
x = 0
repeat 4 times:
    x = x + 3
Trace table
Passx beforex after
No passes run yet.

After 0 passes, x = 0.

Can't use the interactive? Read a description instead

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).

4.Watch

→ 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.

Length: see YouTube · captions on YouTube · source

Nothing loads from YouTube until you press play.

5.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

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.

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.

6.Practice

Practice, not a test. Wrong answers get a hint and another try.

Practice 1

How many counts does it take to count 36 people in groups of 3?

Practice 2

You count 25 people in pairs. How many counts do you need, including the last person on their own?

Practice 3

Algorithm X takes 5 steps and is wrong for one input in ten. Algorithm Y takes 50 steps and is always right. For a lift's safety check, which should you choose?

Practice 4 · a fresh version every time

A robot's counter starts at x = 2. The loop is: repeat until x ≥ 24: x = x + 4. How many times does the loop body run?

7.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?

8.Reflect

Concepts: Algorithms, searching and complexity