Bubble Sort

Sort lists by repeatedly swapping neighbouring items.

  • Define and explain Bubble Sort in your own words
  • Use key terms such as bubble sort accurately
  • Apply what you have learned to new examples and questions
  • Avoid the common mistakes learners make with this topic

This lesson focuses on Bubble Sort: sort lists by repeatedly swapping neighbouring items.

Definition: Bubble Sort

Sort lists by repeatedly swapping neighbouring items.

Key ideas

How bubble sort works

One pass steps through the list comparing each neighbouring pair and swapping them when they are in the wrong order. After a full pass, the largest unsorted item has "bubbled" to its final position at the end. The algorithm repeats passes over the shrinking unsorted section until a complete pass makes no swaps — then the list is sorted. Each pass does about n comparisons, and up to n passes may be needed.

Bubble sort is simple, not fast

Bubble sort is easy to understand and to code, which is why it is taught first — but comparing every pair of neighbours makes it slow, at O(n²) in the worst case. Python's built-in sort uses a far cleverer algorithm called Timsort. In exams, know bubble sort's steps and its weakness.

Key term — bubble sort: Sorting by repeatedly stepping through a list, swapping neighbouring items that are in the wrong order, until no swaps are needed.

Worked example: Bubble Sort

Sort [4, 2, 5, 1] using bubble sort, showing each pass.

Pass 1: compare 4,2 → swap → [2,4,5,1]; compare 4,5 → stay; compare 5,1 → swap → [2,4,1,5]. The 5 has bubbled to the end. Pass 2: compare 2,4 → stay; compare 4,1 → swap → [2,1,4,5]. The 4 is now placed. Pass 3: compare 2,1 → swap → [1,2,4,5]. Pass 4: no swaps — sorted.

Answer: [1, 2, 4, 5] after 4 passes.

Common mistakes
  • Claiming bubble sort is efficient because it is short to write Correction: short code is not fast code — bubble sort's nested comparisons make it O(n²), slow for large lists.
  • Stopping after a single pass Correction: one pass only places the largest item — keep passing until a full pass makes no swaps, otherwise the list is not sorted.

Practice

Trace one pass of bubble sort on [5, 3, 8, 1]. What is the list afterwards?
Compare neighbours left to right, swapping when out of order.

[3, 5, 1, 8] — 5 and 3 swap, 5 and 8 stay, 8 and 1 swap.

How many comparisons does one pass of bubble sort make on a list of 6 items?
Each neighbouring pair is compared once.

5 — the pairs are (1,2), (2,3), (3,4), (4,5), (5,6).

After the first full pass of bubble sort on [7, 3, 9, 2], which item is in its final position?
What bubbles to the end?

9 — the largest item bubbles to the end of the list after one pass.

An algorithm takes 4 seconds for 100 items and 16 seconds for 200 items. What Big-O growth does this suggest?
Doubling the input quadrupled the time.

O(n²) — quadrupling when the input doubles is the signature of quadratic growth.

Quick check

Bubble Sort — quick check

Which of these best defines "bubble sort"?

Sorting by repeatedly stepping through a list, swapping neighbouring items that are in the wrong order, until no swaps are needed.

After one complete pass of bubble sort, what do you know for certain?

The largest unsorted item has bubbled into its final position.
Key takeaways
  • Bubble Sort: sort lists by repeatedly swapping neighbouring items.
  • How bubble sort works: Each pass swaps neighbouring out-of-order pairs, bubbling the largest remaining item to the end; repeat until a pass makes no swaps.
  • bubble sort complexity: Up to n passes of about n comparisons gives O(n²) worst-case time — simple to code, slow on large lists.
  • Watch out for: claiming bubble sort is efficient because it is short to write