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

Sorting algorithms (bubble sort, merge sort)

10 questions51 marksabout 60 min

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

1Standard5 marks

A bubble sort is used to sort [6, 2, 9, 4, 1] into ascending order. Each pass compares adjacent pairs from the start of the list, swapping them if they are out of order. The algorithm stops after a pass in which no swaps are made. No optimisation is made to shorten later passes.

(a) Write the contents of the list at the end of each pass until the algorithm stops. [3]

(b) State the total number of comparisons made. [1]

(c) State the worst-case time complexity of bubble sort. [1]

2Hard5 marks

A merge sort is used to sort [38, 27, 43, 3, 9, 82, 10] into ascending order. When a list has an odd number of items, the extra item goes in the left half.

(a) Show the two lists produced by the first split. [1]

(b) The lists are split until each contains one item, and are then merged back in pairs. Show the lists that exist after the first round of merging (a list with no partner is left unchanged). [2]

(c) Show the final sorted list. [1]

(d) State the time complexity of merge sort. [1]

3Core4 marks

A bubble sort is used to put a list of integers into ascending order.

(a) Describe what happens during one pass of a bubble sort. [2]

(b) State where the largest item in the list will be at the end of the first pass. [1]

(c) A list contains n items. State the maximum number of passes needed to guarantee the list is sorted, if the algorithm does not stop early. [1]

Stuck on Sorting algorithms (bubble sort, merge 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 program stores the names ["Kai", "Ben", "Zoe", "Amy", "Lia", "Dev"] in a list. A bubble sort is used to put them into alphabetical order. Each pass works from the start of the list. The algorithm stops after a pass in which no swaps are made.

(a) Show the contents of the list after the first pass. [2]

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

(c) State the total number of passes made, including the final pass. [1]

5Standard5 marks

The algorithm below is an improved version of a bubble sort. Arrays are zero-indexed.

n ← LEN(arr)
REPEAT
  swapped ← False
  FOR i ← 0 TO n - 2
    IF arr[i] > arr[i + 1] THEN
      temp ← arr[i]
      arr[i] ← arr[i + 1]
      arr[i + 1] ← temp
      swapped ← True
    ENDIF
  ENDFOR
  n ← n - 1
UNTIL swapped = False

(a) Explain the purpose of the variable swapped. [1]

(b) Explain why the line n ← n - 1 makes the algorithm more efficient without making the result incorrect. [2]

(c) The algorithm is run with arr = [3, 5, 8, 1, 9]. State the total number of comparisons made by the IF statement, and the number that would be made if the line n ← n - 1 were removed. [2]

6Standard4 marks

During a merge sort, the two sorted lists [4, 11, 15, 20] and [6, 9, 22] are merged into one list in ascending order.

(a) Show the merged list. [1]

(b) State how many comparisons between items are needed to complete the merge. [1]

(c) Explain why, at each step of the merge, only the first remaining item of each list needs to be compared. [2]

7Hard6 marks

A weather station records 2 million temperature readings each day, which must be sorted. A separate embedded device with very limited RAM keeps a top-10 table that is usually already in order, with at most one new entry out of place.

(a) Explain why a merge sort is more suitable than a bubble sort for sorting the 2 million readings. [3]

(b) Explain two reasons why a bubble sort may be a sensible choice for the top-10 table. [2]

(c) State one disadvantage of a merge sort compared with a bubble sort. [1]

8Hard5 marks

Two sorting algorithms, P and Q, were each run on randomly ordered lists of different sizes. The graph shows the time each took. One algorithm is a bubble sort and the other is a merge sort.

(a) Identify which algorithm is the bubble sort. Justify your answer using the data for P. [2]

(b) Estimate the time algorithm P would take to sort 8000 items. [1]

(c) Algorithm Q took 0.040 s for 1000 items and 0.088 s for 2000 items. Explain why Q's time slightly more than doubles when n doubles. [2]

010002000300040000246810Number of items, nTime taken / s0.62.45.49.6Algorithm P (solid)Algorithm Q (dashed)
9Hard7 marks

The recursive function below performs a merge sort. Merge combines two sorted lists into one sorted list. L[a .. b] is the sub-list from index a to index b inclusive; lists are zero-indexed.

FUNCTION MergeSort(L)
  IF LEN(L) <= 1 THEN
    RETURN L
  ENDIF
  mid ← LEN(L) DIV 2
  left ← MergeSort(L[0 .. mid - 1])
  right ← MergeSort(L[mid .. LEN(L) - 1])
  RETURN Merge(left, right)
ENDFUNCTION

The function is called with MergeSort([52, 17, 64, 8, 33, 90, 25, 41]).

(a) State the total number of times MergeSort is called, including the first call. [1]

(b) State the number of times Merge is called. [1]

(c) State the list returned by the call MergeSort([52, 17, 64, 8]). [1]

(d) Explain why merge sort is described as a divide and conquer algorithm. [2]

(e) Explain why the time complexity of merge sort is O(n log n). [2]

10Stretch6 marks

The improved bubble sort shown below is used. It shortens each pass by one comparison and stops after a pass with no swaps.

n ← LEN(arr)
REPEAT
  swapped ← False
  FOR i ← 0 TO n - 2
    IF arr[i] > arr[i + 1] THEN
      temp ← arr[i]
      arr[i] ← arr[i + 1]
      arr[i + 1] ← temp
      swapped ← True
    ENDIF
  ENDFOR
  n ← n - 1
UNTIL swapped = False

(a) The list [60, 50, 40, 30, 20, 10] is sorted into ascending order. State the total number of comparisons and the total number of swaps made. [2]

(b) Write an expression, in terms of n, for the number of comparisons made when a list of n items in reverse order is sorted. Use it to justify that the worst-case time complexity is O(n²). [2]

(c) Explain what the best-case time complexity of this algorithm is, and the type of input that gives it. [2]

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