Worked solution from the Algorithm design and problem-solving Paper 2 Algorithms, Programming and Logic booklet, Part 6 of 8.
Run the algorithm line by line. It swaps neighbours when the left one is smaller. In the first pass 45 and 56 swap, then 12 and 15 swap, so Flag is 1 and it repeats. The second pass makes no swaps, so Flag stays 0 and it stops.
| Flag | Count | Num[0] | Num[1] | Num[2] | Num[3] | Num[4] | Store |
|---|---|---|---|---|---|---|---|
| 45 | 56 | 30 | 12 | 15 | |||
| 0 | 0 | 45 | |||||
| 56 | |||||||
| 1 | 45 | ||||||
| 1 | |||||||
| 2 | |||||||
| 3 | 12 | ||||||
| 15 | |||||||
| 12 | |||||||
| 0 | 0 | ||||||
| 1 | |||||||
| 2 | |||||||
| 3 |
Flag column correct [1]
Count column correct [1]
Num[0] and Num[1] columns correct [1]
Num[2], Num[3] and Num[4] columns correct [1]
Store column correct [1]
The algorithm sorts (orders) the numbers [1]
into descending order, from largest to smallest. [1]
▶ Whole paper
Video solution of the whole paper · by Quixotic Academy. Tap the picture to open it on YouTube.
Solutions follow the official Cambridge mark schemes; marks are shown in grey. Videos are from other YouTube channels. Paper codes read component / series / year: M/J = May/June, O/N = October/November. Past-paper questions © UCLES, reproduced for classroom practice.