Quelpr

CAPE Computer Science Unit 2 · 2009 · Paper 2 · Question 2(b)(ii)

Binary search on an array.

Given arr containing: 7, 10, 15, 17, 18, 25, 35, 37, 40, 45, 50, 55, 56, 57, 58, 62, 63, 71, 75, 78, 81 and key = 40, show your working to determine how many elements will be checked before returning the correct location.

The mark scheme is shown once you've answered.

Practise this question

Other parts of this question

  1. 2(a)(i)Write a function, lsearch, which takes an integer array arr, its size n, and a search key, performing a linear search to return the index where key is found,…[10 marks]
  2. 2(a)(ii)Assuming key is not in arr, state how many elements of arr are checked before returning -1.[1 mark]
  3. 2(b)(i)Describe the steps performed during a binary search of arr using an algorithm or written explanation, taking the same parameters and return values as linear…[10 marks]

More practice: the rest of this paper · more Searching and Sorting Algorithms questions · all CAPE Computer Science Unit 2 past papers