Phase 1: Prepare
Step 1 Warm-up recall
From Module 4: worst case comparisons for linear search on 50 items?
Private: nothing you type here is sent or stored.
Step 2 Learning goal
By the end of this lesson you can:
- define input size and basic step (Foundation);
- count steps for a simple method (Core);
- write the count as a formula in n (Core).
Phase 2: Teach
Step 3 A familiar example
Timing a method on your own computer tells you about your computer. Counting its steps tells you about the method, and the count does not change when the computer does.
Step 4 Clear explanation
The input size, written n, is the amount of data a method must handle, such as the number of items in a list. A basic step is one simple action, such as one comparison or one addition. The cost of a method is the number of basic steps it takes, written as a formula in n.
For example, to add n numbers you need n − 1 additions, because the first addition joins two numbers and each later one adds one more.
| Numbers to add (n) | Additions needed (n − 1) |
|---|---|
| 10 | 9 |
| 100 | 99 |
| 1,000 | 999 |
Another example: to compare every pair in a list of n items, each item is compared with each later item, giving (n − 1) + (n − 2) + … + 1 comparisons, which equals n × (n − 1) ÷ 2. For 10 items that is 45 comparisons, the same count seen in the sorting methods of Module 4. Counting steps lets you compare methods without a stopwatch, which is why the entry on the algorithm treats cost as part of what a method is.
Step 5 Worked example (4 marks)
How many comparisons does it take to compare every pair among 6 items?
- Use n × (n − 1) ÷ 2 with n = 6. (1 mark)
- 6 × 5 = 30. (1 mark)
- 30 ÷ 2 = 15. (1 mark)
- Answer: 15 comparisons. (1 mark)
Phase 3: Practise
Step 6 Learner practice (instant-marked)
1. The cost of a method is measured by:
Counts are the same on any computer.
2. Adding 100 numbers one pair at a time needs how many additions?
It is n − 1.
3. Comparing every pair among 5 items needs:
5 × 4 ÷ 2 = 10.
4. Input size n means:
It is the amount of data.
Phase 4: Close
Step 7 Recap
- Cost is a count of basic steps.
- Write the count as a formula in n.
- Counts compare methods fairly on any computer.
Step 8 Exit ticket (ungraded)
Count the pair comparisons for 8 items.
Private: nothing you type here is sent or stored.
Step 9 Go further (Extension)
Read the entry on the algorithm and find one other measure of cost besides steps.
Common mistakes
| Mistake | Why it fails |
|---|---|
| Counting time instead of steps | Time depends on the machine; steps do not. |
| Forgetting the first item has no earlier partner | Adding n numbers needs n − 1, not n, additions. |
| Using n instead of n − 1 in the pair formula | Check small cases by hand. |
Limits. Real programs use many kinds of step with different times; counting one kind is a first, useful estimate.