Week 28 · Unit 12: Be a problem solver · 3 lessons
1
Fewer steps, same job
Computing 5: Unit 12, pp. 159–170
GoalSay what makes an algorithm efficient and compare two algorithms that do the same job.
New words
efficient · doing a job with the fewest steps or in the shortest timealgorithm · a set of steps in order that solves a problemrepeat · a block that runs the blocks inside it again and againcompare · to look at two things and say how they are alike and different
Explanation
Two algorithms can solve the same problem in different ways. The more efficient one takes less time or uses fewer steps. To make an algorithm efficient, first break the problem into small parts. Then put the parts together in a good order. A repeat block is a great helper. When the same steps come again and again, one repeat block can replace many copies. The result is the same, but the program is shorter and easier to change. Efficient does not mean sloppy. The shorter algorithm must still do the whole job correctly.
Worked examples
Program
Blocks that draw
Changes for a bigger square
long way
8
4
short way
2 (inside repeat 4)
1
Both programs draw the same square with the pen. The long way has 8 move and turn blocks. The short way has 2 blocks inside one repeat block. To draw a bigger square, the short way needs one change, but the long way needs four.
Algorithm
Walks to the shelf
Walks to the tables
A: one cup at a time
4
4
B: four cups on a tray
1
1
Efficiency matters in real life too. Four tables in a classroom need a cup each. Algorithm A takes one cup at a time. Algorithm B carries all four cups on a tray. Both put 4 cups on the tables, but B uses fewer walks from the shelf.
Algorithm
Steps
Time
X
5
12 minutes
Y
7
9 minutes
Sometimes the number of steps is not the only thing to compare. We can also compare the time. An algorithm with fewer steps that has a long, slow step can still take longer.
Class activity
Move the counters (pairs, 12 counters or buttons, a bowl and a plate). Algorithm A: pick up one counter, put it on the plate, repeat 12 times. Algorithm B: pick up three counters, put them on the plate, repeat 4 times. Count the trips and time each algorithm with a timer. Which is more efficient? What if you could carry 6 counters at a time? Is there a limit to how many you can carry?
Practice
1
Do these two scripts draw the same shape? Which one is more efficient, and why?
Both draw a triangle with sides of 40 steps. The second script is more efficient: it needs 2 blocks inside a repeat block, and the first needs 6 blocks.
2
Algorithm P takes 12 steps. Algorithm Q takes 9 steps. Both do the same job correctly. Which is more efficient by the number of steps? What else could we compare?
Q is more efficient, because it uses fewer steps. We could also compare how much time each one takes.
3
A bus route takes 14 minutes. A second route to the same place takes 11 minutes. Which one is more efficient by time, and how much quicker is it?
Route
Time
first route
14 minutes
second route
11 minutes
The second route is more efficient. It is 3 minutes quicker (14 − 11 = 3).
4
Write a short program with a repeat block that says ‘Hello!’ five times, with a short wait between each one.
when green flag clicked, repeat 5: say Hello! for 1 seconds.