Question bank › IB Computer Science › B2 Programming

Linear and binary search; bubble and selection sort

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

1Core4 marks

A sorted list is stored as data = [3, 8, 12, 19, 25, 31, 40, 47] (indices 0 to 7). A binary search looks for the value 31. Count each examination of a middle item as one comparison. The midpoint is calculated as mid = (low + high) // 2, starting with low = 0 and high = 7.

(a) State the value of mid and the item examined in the first comparison. [2]

(b) Show how low and high change, and state how many comparisons are needed to find 31. [2]

2Standard4 marks

A bubble sort with a swapped flag is applied to [5, 2, 9, 1, 6]. In each pass adjacent items are compared from left to right and swapped if out of order. The algorithm stops after a pass in which no swap occurs.

(a) Write down the list after the first pass. [1]

(b) State the number of swaps made in the first pass. [1]

(c) Determine the total number of passes the algorithm makes, including the final pass. Show the list after each pass. [2]

3Hard5 marks

A sorted list holds 1000 customer ID numbers.

(a) State the maximum number of comparisons a linear search needs. [1]

(b) Calculate the maximum number of comparisons a binary search needs, counting one comparison for each item examined. Show your working. [2]

(c) The list is replaced by an unsorted list of 1000 IDs. Explain why binary search cannot be used on this list, and why sorting it with selection sort just to carry out one search would not be worthwhile. [2]

Stuck on Linear and binary search; bubble and selection sort?

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.

4Core4 marks

A selection sort arranges the list [29, 14, 37, 8, 21, 11] into ascending order. In each pass the smallest item in the unsorted part of the list is found and swapped into the first position of the unsorted part.

(a) Write down the list after each of the first three passes. [3]

(b) State the total number of passes a selection sort makes on a list of 6 items. [1]

5Core4 marks

A social media app stores usernames in a list, for example users = ["kai_02", "zoe.m", "raj99", "zoe.m", "lena_k"]. The list is not in any order.

Construct a function find_user(users, target), in Python or Java, that uses a linear search to return the index of the first occurrence of target in users, or −1 if target is not in the list. [4]

6Standard4 marks

The array cities shown below is sorted into alphabetical order. A binary search is used to look for "Nairobi". It starts with low = 0 and high = 9, uses mid = (low + high) // 2 (integer division), and stops when the item is found or when low > high.

(a) Trace the binary search by stating the values of low, high and mid, and the item examined, at each step. [3]

(b) State the result of the search and the number of items examined. [1]

array cities (index shown above each element)Accra0Berlin1Cairo2Dhaka3Lima4Madrid5Oslo6Paris7Quito8Rome9
7Standard4 marks

A programmer writes the following bubble sort to put a list of daily temperatures into ascending order.

1  def bubble_sort(temps):
2      n = len(temps)
3      for i in range(n - 1):
4          for j in range(n - i):
5              if temps[j] > temps[j + 1]:
6                  temps[j], temps[j + 1] = temps[j + 1], temps[j]
7      return temps

When it is called with bubble_sort([18, 22, 15, 30, 9]) the program crashes.

(a) Identify the line that contains the error and explain why the program crashes. [2]

(b) Write the corrected line. [1]

(c) State the change needed so that the corrected function sorts the temperatures into descending order. [1]

8Hard5 marks

A small sensor device stores readings in flash memory, where every write to memory is slow and wears the memory out. The list [60, 50, 40, 30, 20, 10] must be sorted into ascending order. Two algorithms are being considered:

  • a bubble sort that makes all n − 1 passes, each pass comparing adjacent pairs up to the end of the unsorted part
  • a selection sort that swaps only when the smallest item is not already in position

(a) Calculate the number of swaps made by each algorithm on this list. [2]

(b) Show that both algorithms make the same number of comparisons on this list. [1]

(c) Using your answers, recommend which algorithm should be used on the sensor device. Justify your answer. [1]

(d) State the space complexity of both algorithms and explain why. [1]

9Hard5 marks

A student times a sorting program on lists of random integers of different sizes. The graph shows the measured times, with a dashed curve of best fit.

(a) Describe what happens to the time taken when the number of items is doubled, using values from the graph. [1]

(b) Deduce the time complexity of the algorithm in Big O notation, and state one sorting algorithm from this course that it is consistent with. [1]

(c) Estimate the time to sort 12 000 random items. Show your working. [2]

(d) The program uses a bubble sort with a swapped flag. Explain why sorting 12 000 items that are already in order would take far less time than your answer to (c). [1]

010002000300040000.00.51.01.52.02.50.15 s0.60 s1.35 s2.40 snumber of items, ntime to sort / s
10Stretch6 marks

A quiz app stores players' names and scores in two parallel lists, so that names[i] scored scores[i], for example:

names  = ["Ines", "Tomas", "Priya", "Kofi", "Mei"]
scores = [72, 85, 64, 91, 85]

(a) Construct a procedure sort_scores(names, scores), in Python or Java, that uses a selection sort to arrange the scores into descending order, keeping each name with its score. [5]

(b) State the contents of names after your procedure has run on the example data. [1]

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