Edexcel International Advanced Level Computer Science, point 5.2.3
Definition and function
A bubble sort puts a list in order by repeatedly passing through it, comparing each pair of neighbouring items, and swapping them if they are in the wrong order. The largest values "bubble up" to the end, one more in place on each pass.
Function: it sorts a list into ascending (or descending) order. It is simple, but slow for large lists.
Operation
- Start at the first item.
- Compare it with the next item. If the first is larger, swap them.
- Move on one place and repeat to the end of the list. This is one pass. The largest unsorted item is now in its final place.
- Repeat the pass (each time one fewer item needs checking).
- Stop when a pass makes no swaps: the list is sorted.
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 = n - 1 # last item is now in place
return items
The swapped flag is the early exit: if a pass makes no swaps, the list is already sorted, so the sort stops. Without it, the algorithm would always do every pass.
Hand trace: sort [5, 2, 9, 1]
Pass 1
| Compare | Action | List after |
|---|---|---|
| 5 and 2 | 5 > 2, swap | 2, 5, 9, 1 |
| 5 and 9 | in order | 2, 5, 9, 1 |
| 9 and 1 | 9 > 1, swap | 2, 5, 1, 9 |
Pass 2 (the 9 is in place)
| Compare | Action | List after |
|---|---|---|
| 2 and 5 | in order | 2, 5, 1, 9 |
| 5 and 1 | swap | 2, 1, 5, 9 |
Pass 3
| Compare | Action | List after |
|---|---|---|
| 2 and 1 | swap | 1, 2, 5, 9 |
Pass 4: no swaps, so stop. The sorted list is [1, 2, 5, 9].
Exam questions may ask for the list after each pass, the number of swaps, or the number of comparisons. Here there are 4 swaps in total.
Swapping two items
A swap needs a temporary variable (or a tuple assignment in Python):
temp = a[i]
a[i] = a[i + 1]
a[i + 1] = temp
Writing a[i] = a[i + 1] first and then a[i + 1] = a[i] loses a value.
Efficiency
- A loop inside a loop: in the worst case (a list in reverse order) there are passes, and each checks one item fewer than the last, so the total is comparisons, about . The time still grows with the square of .
- Best case (already sorted, with the flag): one pass, comparisons.
- Space: no extra memory (sorts in place).
| Worst-case comparisons (about ) | |
|---|---|
| 10 | 45 |
| 100 | 4 950 |
| 1 000 | 499 500 |
Benefits and drawbacks
- Benefits: very simple to understand and code, in place (no extra memory), and fast on nearly sorted lists with the early exit.
- Drawbacks: very slow on large lists (time grows with the square of the size), and many swaps are made. Better sorts exist (merge sort, quick sort: 15.2).
Worked example
Task: show the list [4, 3, 1, 2] after each pass of a bubble sort, and count the swaps.
| Pass | Steps | List after | Swaps |
|---|---|---|---|
| 1 | 4,3 swap; 4,1 swap; 4,2 swap | 3, 1, 2, 4 | 3 |
| 2 | 3,1 swap; 3,2 swap | 1, 2, 3, 4 | 2 |
| 3 | 1,2 in order | 1, 2, 3, 4 | 0 |
Pass 3 makes no swaps, so the algorithm stops. 5 swaps in total.
Exam tip: when tracing, show the list at the end of every pass, and remember the largest remaining item ends up in place each time. State the stopping condition (a pass with no swaps). For the drawback, say that it is slow on large lists, because the number of comparisons grows with the square of the number of items.
Check yourself
What happens to the largest item on each pass of a bubble sort?
It moves to the end of the unsorted part (it bubbles up to its final position).
How does a bubble sort know when to stop?
When a complete pass makes no swaps, so the list is in order.
What is the list [6, 2, 5, 1] after one pass?
[2, 5, 1, 6].
Why is a bubble sort slow for large lists?
It uses a loop inside a loop, so the number of comparisons grows with the square of the list size.
Give one advantage of a bubble sort
It is simple to code and sorts in place without extra memory (and is fast on nearly sorted lists with an early exit).