Cambridge A2 Level Computer Science 9618 · 19.1 Algorithms

Q21 · 9618/33/M/J/25 Q12

Worked solution from the Computational thinking and Problem-solving Paper 3 Advanced Theory booklet, Part 1 of 2.

Q219618/33/M/J/25original Q12Structured · 7 marks
Question 12 from 9618/33/M/J/25

Worked solution

(a)

Write two ADTs.

Any two of these: stack, queue, linked list, binary tree, graph [1]

(b)

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 Index

Value ← 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.

(c)

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]

KEY IDEAInsertion sort keeps one value aside, shifts larger values along, then inserts it; sort speed depends on how ordered and how large the data is.
TOTAL 7 marks

Official mark scheme

Mark scheme for 9618/33/M/J/25 question 12
VIDEO
▶ 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.