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.
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
| Position in list | Book code | Order examined |
|---|---|---|
| 1 | ART02 | |
| 2 | BIO12 | |
| 3 | CHE15 | |
| 4 | ENG22 | |
| 5 | GEO31 | |
| 6 | HIS44 | |
| 7 | MAT51 | |
| 8 | PHY60 |