Skip to content
MathsGenie logo
Open app

Course home

  1. IGCSE
  2. Computer Science Edexcel
  3. Question bank

Algorithms

EasyMedium
123456789101112131415161718
Question 18

Figure 2 shows an algorithm for a binary search.

This binary search algorithm will be used to search the library inventory list for the book code MAT51.

Complete the table to indicate the order in which the book codes will be examined by the algorithm.

Write the number 1 by the first book code to be examined, 2 by the second code to be examined, and so on.

Figure 2

1  #   Initialise variables
2  SET searchCode TO " "
3  SET low TO 1
4  SET mid TO 0
5  SET high TO LENGTH(bookList)
6  SET found TO False
7
8  #   Obtain search code from user
9  SEND "Enter book code to search" TO DISPLAY
10 RECEIVE searchCode FROM (STRING) KEYBOARD
11 REPEAT
12     mid = (low + high) DIV 2
13     IF bookList[mid] < searchCode THEN
14         low = mid + 1
15     END IF
16     IF bookList[mid] > searchCode THEN
17         high = mid - 1
18     END IF
19 UNTIL bookList[mid] = searchCode OR low = high

Book list table

Position in listBook codeOrder examined
1ART02
2BIO12
3CHE15
4ENG22
5GEO31
6HIS44
7MAT51
8PHY60
[4]

Algorithms Questions

  1. IGCSE
  2. /Computer Science
  3. /Algorithms