Worked solution from the Computational thinking and Problem-solving Paper 3 Advanced Theory booklet, Part 1 of 2.
Write two ADTs.
Any two of these: stack, queue, linked list, binary tree, graph [1]
Take each item in turn. Keep it in Value. Shift larger items one place right, then drop Value into the gap.
FOR Index ← 2 TO 1000
Value ← DataArray[Index]
Position ← Index - 1
WHILE Position >= 1 AND DataArray[Position] > Value
DataArray[Position + 1] ← DataArray[Position]
Position ← Position - 1
ENDWHILE
DataArray[Position + 1] ← Value
NEXT IndexValue ← DataArray[Index] before the inner loop [1]
Inner loop (WHILE) that checks DataArray[Position] > Value [1]
Moving the data to the next element and updating Position inside the loop [1]
Value put back with DataArray[Position + 1] ← Value after the loop [1]
Other correct insertion sorts also score, for example a FOR inner loop with an IF.
Write two ways the data affects speed.
A sort is quicker if the data is already partially sorted, and likely slower if it is completely out of order [1]
A sort takes longer when there are more items to sort, and less time when there are fewer [1]
▶ Topic lesson
A lesson on this topic · by rjmcompsci. 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.