Common algorithm patterns
Sum, count, max/min, search, sort — all show up on Paper 2.
Running total / sum.
Total ← 0
FOR Counter ← 1 TO N
INPUT Number
Total ← Total + Number
NEXT Counter
OUTPUT Total
Count items meeting a condition.
Count ← 0
FOR Counter ← 1 TO N
INPUT X
IF X > 50 THEN
Count ← Count + 1
ENDIF
NEXT Counter
OUTPUT Count
Find the maximum.
INPUT Number
Max ← Number
FOR Counter ← 2 TO N
INPUT Number
IF Number > Max THEN
Max ← Number
ENDIF
NEXT Counter
OUTPUT Max
Find the minimum. Same pattern, with < instead of > and Min instead of Max.
Average. Compute the running total then divide by N.
Total ← 0
FOR Counter ← 1 TO N
INPUT Number
Total ← Total + Number
NEXT Counter
Average ← Total / N
OUTPUT Average
Cambridge tip. Memorise the patterns so you don't have to invent them in the exam. Most Paper 2 algorithm questions are variations on these.
- Running total: Total ← Total + Number.
- Counter: Count ← Count + 1.
- Max: initialise to FIRST input, not 0.
- Average: total / count.
See the full worked example for writing algorithms and identifying errors →