Cambridge A2 Level Computer Science 9618 · 19.1 Algorithms

Q15 · 9618/33/M/J/24 Q10

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

Q159618/33/M/J/24original Q10Structured · 8 marks
Question 10 from 9618/33/M/J/24

Worked solution

(a)

The array must be in order.

The elements must be sorted (in ascending or descending order) [1]

(b)

Set Found before the loop. The loop runs while the item is not found and the list is not used up. Test the middle item, then move the top up or the end down.

Found ← FALSE
NotInList ← FALSE
WHILE Found = FALSE AND NotInList = FALSE
    CurrentItem ← (TopOfList + EndOfList) DIV 2
    IF ToFind = Names[CurrentItem] THEN
        Found ← TRUE
    ELSE
        IF TopOfList >= EndOfList THEN
            NotInList ← TRUE
        ELSE
            IF ToFind > Names[CurrentItem] THEN
                TopOfList ← CurrentItem + 1
            ELSE
                EndOfList ← CurrentItem - 1
            ENDIF
        ENDIF
    ENDIF
ENDWHILE

Found ← FALSE [1]

WHILE Found = FALSE AND NotInList = FALSE [1]

IF ToFind = Names[CurrentItem] THEN [1]

NotInList ← TRUE [1]

TopOfList ← CurrentItem + 1 [1]

Only the five blanks are marked.

(c)

Binary search halves the search area each time, so it is very fast on large arrays.

The Big O of a binary search is O(log2 n) [1]

The time taken grows logarithmically as the number of items grows: the search area is halved each time, so only a small number of comparisons are needed [1]

Another accepted point: Big O notation shows the time or space complexity of an algorithm.

KEY IDEABinary search needs sorted data and halves the search area each step, giving O(log n) time.
TOTAL 8 marks

Official mark scheme

Mark scheme for 9618/33/M/J/24 question 10
VIDEO
▶ Whole paper

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