Question 2

OCR Computer Science November 2021 Algorithms and programming (H446-02)A level14 marks

On the specification

(a)(i)
2.3 Algorithms
(a)(ii)
2.2 Problem solving and programming
(b)
2.3 Algorithms
(c)
2.3 Algorithms
Question text

2 The pseudocode function binarySearch() performs a binary search on the array dataArray that is passed as a parameter. The function returns the array index of searchValue within the array, and -1 if it is not in the array. (a) The pseudocode binary search algorithm is incomplete. (i) Complete the algorithm by filling in the missing statements. function binarySearch(dataArray:byref, upperbound, lowerbound, ) while true middle = lowerbound + ((upperbound – lowerbound) ) if upperbound < lowerbound then return else if dataArray[middle] < searchValue then lowerbound = elseif dataArray[middle] > searchValue then upperbound = else return endif endif endwhile endfunction [6] (ii) The algorithm uses a while loop. State a different type of loop that could be used instead of the while loop in the given algorithm. [1] (b) The tables below show possible Big O complexities for the worst-case space, best-case space and average time for search algorithms. Tick the worst-case space complexity for a binary and linear search. Binary Linear search search O(log(n)) O(1) O(n) Tick the best-case space complexity for a binary and linear search. Binary Linear search search O(log(n)) O(1) O(n) Tick the average time complexity for a binary and linear search. Binary Linear search search O(log(n)) O(1) O(n) [6] (c) Identify one situation where a linear search is more appropriate than a binary search. [1]