Bubble sort repeatedly compares adjacent pairs in a list and swaps them if they are in the wrong order. After each pass, the largest unsorted item "bubbles" to its correct position at the end. Repeat until no swaps are needed.
Pass 1 moves the largest item to the last position
Pass 2 moves the second largest to second-to-last
A list of n items requires at most n-1 passes
If no swaps occur in a pass → list is sorted → stop early
Worked Example
Sorting: [5, 3, 8, 1, 9, 2]
Pass 1: [3, 5, 1, 8, 2, 9] — 9 bubbles to end
Pass 2: [3, 1, 5, 2, 8, 9] — 8 moves to position
Pass 3: [1, 3, 2, 5, 8, 9] — 5 moves to position
Pass 4: [1, 2, 3, 5, 8, 9] — sorted!
Exam tip:Edexcel may ask you to trace bubble sort — show each pass and the list state after each one.
Efficiency Analysis
How Good is Bubble Sort?
Best case: list already sorted → only 1 pass with 0 swaps → O(n)
Worst case: list in reverse order → n-1 passes, many swaps → O(n²)
Bubble sort is simple but inefficient for large lists. Merge sort (O(n log n)) is preferred for large datasets.
Exam Practice
Have a go at this question
Edexcel-style question
Trace one complete pass of bubble sort on the list: [7, 4, 2, 9, 1]. Show the list after the pass and state how many swaps were made.