Skip to main content
EnglandComputer ScienceSyllabus dot point

How do bubble sort and merge sort work, and which is more efficient?

Understand the bubble sort and merge sort algorithms, how each orders a list, their time complexity, and the trade-off between them.

A focused answer to AQA A-Level Computer Science 4.3.3, covering the bubble sort and merge sort algorithms, how each works, their time complexity, and the trade-off between simplicity and efficiency.

Generated by Claude Opus 4.88 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. Bubble sort
  3. Merge sort
  4. The trade-off

What this dot point is asking

AQA wants you to describe how bubble sort and merge sort work, give the time complexity of each, trace a pass, and explain the trade-off between the simple bubble sort and the more efficient merge sort.

Bubble sort

FOR pass = 1 TO n - 1
  swapped = False
  FOR i = 0 TO n - 1 - pass
    IF list[i] > list[i + 1] THEN
      swap(list[i], list[i + 1])
      swapped = True
  IF NOT swapped THEN break   # already sorted

Bubble sort has worst- and average-case time complexity O(n2)O(n^2) because of the nested loops: roughly nn passes each doing up to nn comparisons. The best case, on an already sorted list with the early-exit flag, is O(n)O(n) because a single clean pass detects that no swaps were needed. It sorts in place (no extra memory beyond a temporary swap variable), which is its main appeal, but it is impractical for large lists.

Merge sort

mergeSort(list):
  IF length <= 1 THEN RETURN list
  split into left and right halves
  left = mergeSort(left)
  right = mergeSort(right)
  RETURN merge(left, right)   # combine in sorted order

The merge step is where the ordering happens: two already-sorted sublists are combined by repeatedly taking the smaller of the two front items, which is why each merge level is linear in the number of items.

The trade-off

Bubble sort is easy to understand and uses no extra memory, but its O(n2)O(n^2) growth makes it too slow for large data. Merge sort is harder to code and uses additional memory, but its O(nlogn)O(n \log n) growth makes it the right choice for large lists. This illustrates the general trade-off between simplicity and efficiency, and the related trade-off between time and space.

To see the gap in practice, consider sorting a list of one million items. Bubble sort performs on the order of n2=1012n^2 = 10^{12} comparisons, which is around a trillion operations and would take an unacceptably long time. Merge sort performs on the order of nlogn106×20=2×107n \log n \approx 10^6 \times 20 = 2 \times 10^7 operations, around twenty million, which a modern processor handles in a fraction of a second. The difference is not a constant factor that faster hardware can overcome; it grows with the data, which is exactly the point Big-O is making.

There is also a difference in adaptiveness. Bubble sort with the early-exit flag is adaptive: on a list that is already sorted it makes a single O(n)O(n) pass and stops, which is why it is occasionally used to detect whether a small list is in order. Merge sort always does the full O(nlogn)O(n \log n) work regardless of the starting arrangement, because it splits and merges the whole list every time. Merge sort is also a stable sort, meaning equal elements keep their original relative order, a property that matters when sorting records by one field while preserving an earlier ordering on another. These finer points (adaptiveness and stability) are the kind of detail that distinguishes a top-band answer from a bare recall of the two complexities.

Exam-style practice questions

Practice questions written in the style of AQA exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

AQA 20194 marksShow the result of the first complete pass of a bubble sort on the list [5, 2, 9, 1, 6], comparing adjacent elements from the left. State the list after each swap during the pass.
Show worked answer →

Start: [5, 2, 9, 1, 6]. Compare 5 and 2: out of order, swap to [2, 5, 9, 1, 6]. Compare 5 and 9: in order, no swap. Compare 9 and 1: swap to [2, 5, 1, 9, 6]. Compare 9 and 6: swap to [2, 5, 1, 6, 9].

After the first pass the list is [2, 5, 1, 6, 9], and the largest value, 9, has bubbled to the end.

Markers reward each correct adjacent comparison and swap, and recognising that one full pass guarantees the largest unsorted element reaches its final position at the end.

AQA 20224 marksExplain why merge sort has a time complexity of O(n log n) and discuss one disadvantage of merge sort compared with bubble sort.
Show worked answer →

Merge sort repeatedly halves the list until single items remain, which takes about log2n\log_2 n levels of splitting. At every level the total work of merging the sublists back together touches all nn items once, which is O(n)O(n) per level. Multiplying the O(n)O(n) work per level by the logn\log n levels gives O(nlogn)O(n \log n) overall, in all cases.

A disadvantage is that merge sort is not in place: it allocates temporary sublists to perform the merges, so it uses O(n)O(n) extra memory, whereas bubble sort sorts in place with only a constant amount of extra space. Merge sort is also more complex to implement.

Markers reward the level-times-work argument for O(nlogn)O(n \log n) and a valid disadvantage (extra memory or greater complexity).

Related dot points

Sources & how we know this