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.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
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 because of the nested loops: roughly passes each doing up to comparisons. The best case, on an already sorted list with the early-exit flag, is 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 growth makes it too slow for large data. Merge sort is harder to code and uses additional memory, but its 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 comparisons, which is around a trillion operations and would take an unacceptably long time. Merge sort performs on the order of 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 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 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 levels of splitting. At every level the total work of merging the sublists back together touches all items once, which is per level. Multiplying the work per level by the levels gives 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 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 and a valid disadvantage (extra memory or greater complexity).
Related dot points
- Understand the linear search and binary search algorithms, how each works, their requirements, and their time complexity.
A focused answer to AQA A-Level Computer Science 4.3.2, covering the linear search and binary search algorithms, how each works, the requirement that binary search needs sorted data, and their time complexity.
- Understand Big-O notation for time and space complexity, the common orders of growth, how to determine the complexity of an algorithm, and the meaning of best, average and worst case.
A focused answer to AQA A-Level Computer Science 4.3.5, covering Big-O notation for time and space complexity, the common orders of growth, determining the complexity of an algorithm, and best, average and worst case.
- Understand depth-first and breadth-first graph traversal, the data structures they use, and the pre-order, in-order and post-order tree traversal algorithms.
A focused answer to AQA A-Level Computer Science 4.3.1, covering depth-first and breadth-first graph traversal and the data structures they use, plus the pre-order, in-order and post-order tree traversal algorithms.
- Understand arrays (one, two and three dimensional), records and fields, and the difference between static and dynamic data structures.
A focused answer to AQA A-Level Computer Science 4.2.1, covering one, two and three dimensional arrays, records and fields, indexing, and the difference between static and dynamic data structures.
- Understand Dijkstra's shortest path algorithm, how it finds the lowest-cost path from a start vertex, the role of a priority queue, and its applications.
A focused answer to AQA A-Level Computer Science 4.3.4, covering Dijkstra's shortest path algorithm, how it finds the lowest-cost path from a start vertex in a weighted graph, the role of a priority queue, and its applications.
Sources & how we know this
- AQA A-level Computer Science (7517) specification — AQA (2015)