AQA 7517, section 4.3.5.1
Bubble sort repeatedly compares neighbouring items and swaps them if they are in the wrong order. After each pass the largest unsorted item has "bubbled" to the end of the list, like an air bubble rising to the surface.
It is included in the specification as an example of a particularly inefficient sorting algorithm, which is useful for comparing with faster ones.
Algorithm
- Start at the beginning of the list.
- Compare the first two items. If the first is larger, swap them.
- Move along one place and compare the next pair. Carry on to the end of the list. This is one pass.
- Repeat the passes. Stop when a whole pass makes no swaps, because then the list is sorted.
Each pass puts the next largest item in its final place at the end, so later passes can skip the items that are already sorted.
Trace
Sort into ascending order:
| Pass | Comparisons and swaps | List after the pass |
|---|---|---|
| 1 | 5,2 swap. 5,9 no swap. 9,1 swap. | 2, 5, 1, 9 |
| 2 | 2,5 no swap. 5,1 swap. | 2, 1, 5, 9 |
| 3 | 2,1 swap. | 1, 2, 5, 9 |
| 4 | No swaps, so the list is sorted. | 1, 2, 5, 9 |
After pass 1 the largest value, 9, is at the end. After pass 2, 5 is in its final place, and so on.
In Python
def bubble_sort(items):
n = len(items)
swapped = True
while swapped:
swapped = False
for i in range(n - 1):
if items[i] > items[i + 1]:
items[i], items[i + 1] = items[i + 1], items[i]
swapped = True
n -= 1 # the largest item is now in its final place
return items
print(bubble_sort([5, 2, 9, 1])) # [1, 2, 5, 9]
The swapped flag lets the algorithm stop early once a pass makes no swaps. Without it, the algorithm would always do every pass, even if the list became sorted early.
Swapping two items needs care. In Python, a, b = b, a swaps in one line. In many languages you need a temporary variable to hold one value while you swap:
temp = items[i]
items[i] = items[i + 1]
items[i + 1] = temp
Time complexity
For a list of items, the first pass makes comparisons, the next , and so on. In total that is about comparisons, which grows with the square of .
| Case | When | Time complexity |
|---|---|---|
| Worst | The list is in reverse order | |
| Average | A random order | |
Best (with the swapped flag) |
The list is already sorted: one pass, no swaps |
The key fact for the exam is that bubble sort is . If you double the number of items, the time roughly quadruples. For 1 000 items that is about half a million comparisons, and for 1 000 000 items it is about half a trillion.
- Memory: it sorts in place, using almost no extra memory, just a variable for the swap.
- It is simple to write and understand, but it is too slow for large lists.
Worked example
Task: show the list after each pass of bubble sort on , and count the swaps.
| Pass | Swaps | List after the pass |
|---|---|---|
| 1 | 4,3 swap. 4,2 swap. 4,1 swap. (3 swaps) | 3, 2, 1, 4 |
| 2 | 3,2 swap. 3,1 swap. (2 swaps) | 2, 1, 3, 4 |
| 3 | 2,1 swap. (1 swap) | 1, 2, 3, 4 |
| 4 | No swaps | 1, 2, 3, 4 |
The reversed list is the worst case. There are 6 swaps in total, which is , and a fourth pass is needed to confirm that nothing more needs to change.
Exam tip: when asked to trace a sort, show the list after each pass and say how many swaps were made. A final pass with no swaps is what stops the algorithm, so include it. For complexity, say that bubble sort is because of the nested comparisons (a loop within a loop).
Check yourself
Show the list after each pass of bubble sort on [3, 1, 2]
Pass 1: 1, 2, 3 (3,1 swap, then 3,2 swap). Pass 2: 1, 2, 3 (no swaps), so it is sorted.
What is the time complexity of bubble sort, and why?
. There are about passes, and each one makes up to comparisons, so the number of comparisons grows with the square of .
What is the purpose of the swapped flag?
It records whether any swaps were made in a pass. If none were made, the list is already sorted and the algorithm can stop early.
When is bubble sort quick?
When the list is already sorted, or very nearly sorted, because with the flag it needs only one or two passes, which is .