Cambridge A2 Level Computer Science 9618 · 19.1 Algorithms

Q5 · 9618/31/O/N/22 Q12

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

Q59618/31/O/N/22original Q12Structured · 9 marks
Question 12 from 9618/31/O/N/22

Worked solution

(a)

Upper must start at the last index, 99. Stop if Upper drops below Lower. Move Lower or Upper past Mid. Stop when the name is found.

Lower ← 0
Upper ← 99
Mid ← 0
Exit ← FALSE
OUTPUT "Enter the name to be found "
INPUT Target
REPEAT
    IF Upper < Lower THEN
        OUTPUT Target, " does not exist"
        Exit ← TRUE
    ENDIF
    Mid ← Lower + (Upper - Lower + 1) DIV 2
    IF Names[Mid] < Target THEN
        Lower ← Mid + 1
    ENDIF
    IF Names[Mid] > Target THEN
        Upper ← Mid - 1
    ENDIF
    IF Names[Mid] = Target THEN
        OUTPUT Target, " was found at location ", Mid
        Exit ← TRUE
    ENDIF
UNTIL Exit

Upper ← 99 [1]

IF Upper < Lower THEN [1]

Lower ← Mid + 1 [1]

Upper ← Mid - 1 [1]

IF Names[Mid] = Target THEN [1]

UNTIL Exit (or UNTIL Exit = TRUE) [1]

(b)(i)

Write:

O(n) [1]

(b)(ii)

Write any two of these:

O(log n) means the algorithm takes logarithmic time [1]

The time taken goes up linearly while the number of items rises exponentially [1]

Other points that also score: it is the worst case time complexity for a binary search.

KEY IDEAA binary search needs Upper, Lower and Mid updated each time, and its time grows with log n, not n.
TOTAL 9 marks

Official mark scheme

Mark scheme for 9618/31/O/N/22 question 12
VIDEO
▶ Whole paper

Video solution of the whole paper · by MAK CS. 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.