9618/32/M/J/24 question 8: worked solution (May/June 2024, paper 32)

Show as plain text

Cambridge A2 Level Computer Science 9618 · 19.1 Algorithms Worked solution from the Computational thinking and Problem-solving Paper 3 Advanced Theory booklet, Part 1 of 2. Q14 9618/32/M/J/24 original Q8 Structured · 8 marks 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(log₂ n) [1] Big O notation shows the time or space complexity of an algorithm. Any four correct points earn the marks. KEY IDEA A 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 VIDEO ▶ Starts at Q8 (56:55) Starts at Q8 (56:55) · by Pseudocode Pro. Tap the picture to open it on YouTube.