Cambridge A2 Level Computer Science 9618 · 19.1 Algorithms

Q14 · 9618/32/M/J/24 Q8

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

Q149618/32/M/J/24original Q8Structured · 8 marks
Question 8 from 9618/32/M/J/24

Worked solution

(a)

Set Found before the loop. Keep looping while the item has not been found and the list is not used up. Compare each element in turn.

Found ← FALSE
NotInList ← FALSE
Count ← TopOfList
WHILE Found = FALSE AND NotInList = FALSE
    IF ToFind = Widgets[Count] THEN
        Found ← TRUE
    ENDIF
    Count ← Count + 1
    IF Found = FALSE AND Count > EndOfList THEN
        NotInList ← TRUE
    ENDIF
ENDWHILE

Found ← FALSE [1]

WHILE Found = FALSE AND NotInList = FALSE (or Count <= EndOfList for the second test) [1]

IF ToFind = Widgets[Count] THEN [1]

IF Found = FALSE AND Count > EndOfList THEN [1]

(b)

Compare how each search works, then give the Big O of each. Any four of these points, with no more than three about the methods and no more than two about Big O:

A linear search checks each element in turn from the start until the item is found or the end is reached [1]

A binary search looks at the middle element and discards the half that cannot hold the item, repeatedly halving the search area [1]

A binary search needs the elements sorted and usually makes many fewer comparisons than a linear search, which needs no sorting [1]

Big O: linear search is O(n) and binary search is O(log2 n) [1]

Big O notation shows the time or space complexity of an algorithm. Any four correct points earn the marks.

KEY IDEAA linear search checks items one by one, O(n); a binary search halves a sorted list each time, O(log n).
TOTAL 8 marks

Official mark scheme

Mark scheme for 9618/32/M/J/24 question 8
VIDEO
▶ Starts at Q8 (56:55)

Starts at Q8 (56:55) · by Pseudocode Pro. 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.