Skip to content
MathsGenie logo
Open app

Course home

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

Algorithms

EasyMedium
123456789101112131415161718
Question 16

A programmer is writing a Python-style algorithm to carry out a binary search on a sorted list of book registration IDs, book_ids, to locate a specific ID stored in search_id.

The programmer has started writing the script but has left some lines blank.

def binary_search(book_ids, search_id):
    start = 0
    end = len(book_ids) - 1
    found = False
    # Line 5: Initialize the comparisons counter
    ____________________

    while start <= end and not found:
        # Line 8: Increment the comparisons counter
        ____________________

        # Line 10: Calculate the middle index (using integer division)
        ____________________

        if book_ids[middle] == search_id:
            found = True
        elif search_id < book_ids[middle]:
            # Line 15: Adjust the end pointer
            ____________________
        else:
            # Line 17: Adjust the start pointer
            ____________________

    return found, comparisons

Amend the code by providing the correct statements to fill the blank spaces on:

a.
  • Line 5
[2]
b.
  • Line 8
[2]
c.
  • Line 10
[2]
d.
  • Line 15
[2]
e.
  • Line 17

Maintain the correct logic of a binary search and track the loop execution count.

[2]

Algorithms Questions

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