The list below is sorted in ascending order, with indexes starting at 0.
[3, 8, 12, 17, 23, 29, 35, 41, 50]
A binary search uses mid = (low + high) DIV 2, starting with low = 0 and high = 8.
(a) Show the values of mid and the item examined at each step when searching for 12. [2]
(b) State why a binary search cannot be used on an unsorted list. [1]
- (a) Step 1: mid = 4, item 23 examined (the comparison and new low/high values shown in brackets here are not required for the mark: 12 < 23 so high becomes 3)(1)
- (a) Step 2: mid = 1, item 8 examined (12 > 8 so low becomes 2); step 3: mid = 2, item 12 examined and found – both mid values 1 and 2 (with items 8 and 12) needed for the mark; ignore extra working such as low/high values(1)
- (b) The algorithm relies on the items being in order to decide which half of the list to discard at each step; in an unsorted list the item could be in the discarded half (allow: it could not tell whether the item is before or after the middle)(1)
- Answer: (a) mid = 4 (23), mid = 1 (8), mid = 2 (12, found) (b) It relies on the ordering to discard half the list.
- Examiner tip: Show every mid value and the item at it – examiners award the method marks for the sequence, not just the final index.