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

Searching algorithms (linear, binary, binary tree)

10 questions44 marksabout 55 min

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]

3Core3 marks

A linear search is carried out on the unsorted list below, starting at the first item and stopping as soon as the target is found.

[14, 7, 31, 22, 9, 40, 18]

(a) State how many items are compared with the target when searching for 22. [1]

(b) State how many items are compared with the target when searching for 15. [1]

(c) Give one reason why a linear search, rather than a binary search, must be used on this list. [1]

Stuck on Searching algorithms (linear, binary, binary tree)?

Get it sorted in a free 30-minute session

We work through your weakest questions on this topic on Zoom. I reply the same day.

4Core3 marks

Figure 1 shows a binary search tree.

(a) List, in order, the nodes that are visited when the tree is searched for 47. [1]

(b) The tree is searched for 66. List, in order, the nodes that are visited and explain how the algorithm determines that 66 is not in the tree. [2]

50307218416485925476893
5Standard4 marks

The pseudo-code below is intended to carry out a linear search of the array Items (indexes starting at 0). It should return the index of the first occurrence of Target, or −1 if Target is not present. Four parts are missing, labelled A to D.

SUBROUTINE LinearSearch(Items, Target)
  i ← 0
  Found ← False
  WHILE i < LEN(Items) AND ____A____
    IF ____B____ THEN
      Found ← True
    ELSE
      ____C____
    ENDIF
  ENDWHILE
  IF Found = True THEN
    RETURN i
  ELSE
    RETURN ____D____
  ENDIF
ENDSUBROUTINE

State what should be written in each of the gaps A to D. [4]

6Standard5 marks

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

[4, 9, 15, 21, 26, 33, 38, 44, 52, 57, 63, 70]

An iterative binary search sets low ← 0 and high ← 11, then repeats while low ≤ high: it calculates mid ← (low + high) DIV 2; if Data[mid] is the target it returns mid; if the target is greater than Data[mid] it sets low ← mid + 1, otherwise it sets high ← mid − 1. If the loop ends without finding the target it returns −1.

(a) Complete a trace of the search for the target 40, showing the values of low, high and mid and the item Data[mid] at each step, and the value returned. [4]

(b) Explain why the condition low > high shows that the target is not in the list. [1]

7Standard4 marks

Two searching algorithms, X and Y, were tested on lists of different sizes. The table shows the greatest number of comparisons each algorithm needed (the worst case) for each list size. One algorithm is a linear search and the other is a binary search.

Number of items, nXY
1000100010
2000200011
4000400012
8000800013

(a) Identify which algorithm is the binary search. Justify your answer using the data in the table. [2]

(b) Predict the worst-case number of comparisons for algorithms X and Y when n = 64 000. [2]

8Hard5 marks

A program must search an unsorted list of 1 000 000 items many times. Two approaches are considered:

  • Approach A: carry out a linear search of the list for every search.
  • Approach B: sort the list once using merge sort, then carry out a binary search for every search.

Assume that, in the worst case, the merge sort needs 20 000 000 comparisons, each binary search needs 20 comparisons and each linear search needs 1 000 000 comparisons.

(a) Calculate the smallest number of searches for which Approach B uses fewer comparisons in total than Approach A. Show your working. [3]

(b) Items are frequently added to the list between searches. Explain why this makes Approach B less attractive. [2]

9Hard4 marks

The values 10, 20, 30, 40, 50, 60, 70 are inserted, in that order, into an empty binary search tree.

(a) State the number of nodes that must be visited to find the value 70 in this tree. [1]

(b) The same seven values are instead inserted in the order 40, 20, 60, 10, 30, 50, 70. State the number of nodes that must be visited to find 70 in this tree. [1]

(c) Binary tree search is often described as having a time complexity of O(log n). With reference to your answers to parts (a) and (b), explain why this is not always true. [2]

10Stretch6 marks

A binary search tree of strings is stored in three arrays. Left and Right hold the index of a node's left and right child, with −1 meaning there is no child. The root is at index 0.

IndexDataLeftRight
0M12
1F34
2T5−1
3B−1−1
4J6−1
5P−17
6H−1−1
7R−1−1

(a) State the indexes of the nodes visited, in order, when the tree is searched for R. [1]

(b) Write an iterative subroutine TreeSearch(Target) that searches this tree and returns True if Target is in the tree and False otherwise. The arrays Data, Left and Right are global. You may write your answer in pseudo-code or a programming language you have studied. [5]

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