Worked solution from the Computational thinking and Problem-solving Paper 3 Advanced Theory booklet, Part 1 of 2.
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
ENDWHILEFound ← 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]
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.
▶ 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.