9618/31/O/N/22 question 12: worked solution (October/November 2022, paper 31)

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. Q5 9618/31/O/N/22 original Q12 Structured · 9 marks 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 IDEA A 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 VIDEO ▶ Whole paper Video solution of the whole paper · by MAK CS. Tap the picture to open it on YouTube.