Question bank › A-level Computer Science › Fundamentals of algorithms

Searching algorithms (linear, binary, binary tree)

Answer on paper first, then reveal the mark scheme one mark at a time.

1Core3 marks

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]

2Stretch7 marks

A sorted list Data is stored in an array with indexes starting at 0.

(a) Write a recursive subroutine BinarySearch(Data, target, low, high) that returns the index of target within Data[low..high], or −1 if it is not present. The middle index must be found using (low + high) DIV 2. You may write your answer in pseudo-code or a programming language you have studied. [4]

(b) The list contains 16 items. State the greatest number of items that need to be examined by the search in the worst case. [1]

(c) Explain why a recursive binary search uses more memory than an iterative binary search of the same list. [2]

Stuck on this topic?

Book a free 30-minute session on WhatsApp and we'll find exactly which step is costing you the marks. Or build a timed paper on it.

Free 30-minute session