Each pass walks left to right and swaps any two neighbours that are out of order. Watch the right end: it fills up with sorted values, one per pass.
Quick check
Quick check: After pass 1, which value is sure to be in its final place?
Why the biggest always reaches the end#
Here is the list before pass 1, and then after it.
Compare the two lists. 5 moved right until 8 stopped it. Then 8 moved until 9 stopped it, 9 moved until 10 stopped it, and 10 swapped its way to the last slot. The biggest value left is bigger than every neighbour it meets, so it never stops. Pass 2 does the same for the next biggest, 9, and parks it just left of 10. So pass p puts the p-th biggest value in place, and the last p slots never need checking again. That is why pass p needs only n - p compares.
Quick check
Quick check: 10 values. Pass 1 made 9 compares. How many does pass 2 need?
Counting passes and compares#
After n - 1 passes, the n - 1 biggest values are in place, so the one left at index 0 must be the smallest. That makes at most n - 1 passes. Add up the compares: (n - 1) + (n - 2) + ... + 1 = n(n - 1) / 2. Check with n = 4: 3 + 2 + 1 = 6, and 4 × 3 / 2 = 6. Pairing the first and last terms always gives n, and there are (n - 1) / 2 such pairs.
| Values (n) | Passes at most | Compares at most |
|---|---|---|
| 4 | 3 | 3 + 2 + 1 = 6 |
| 5 | 4 | 10 |
| 10 | 9 | 45 |
| n | n - 1 | n(n - 1) / 2 |
Quick check
Quick check: A placement cell sorts 100 CGPAs with bubble sort. At most how many compares?
The code, line by line#
Here is bubble sort on [4, 1, 3, 2]. swapped remembers whether a pass changed anything. If a whole pass makes no swaps, every neighbour pair is in order, so the list is sorted and we stop. That is the early exit.
- a = [4, 1, 3, 2]
- n = len(a)
- for p in range(1, n):
- swapped = False
- for j in range(n - p):
- if a[j] > a[j+1]:
- a[j],a[j+1] = a[j+1],a[j]
- swapped = True
- if not swapped:
- break
- print(a)
Variables
a
Four values to sort. The biggest, 4, is first.
That run made 3 passes, 6 compares and 4 swaps. The break came on the last pass, so it saved nothing here. Now give it a list that is already sorted. Drag n to 6 and watch it stop after one pass.
n- n = 4
- a = list(range(1, n + 1))
- for p in range(1, n):
- swapped = False
- for j in range(n - p):
- if a[j] > a[j+1]:
- a[j],a[j+1] = a[j+1],a[j]
- swapped = True
- if not swapped:
- break
- print(a)
Variables
- n
- 4 (just changed)
Pick n. The list will be 1 to n, in order.
Quick check
Quick check: With n = 1, how many compares does the code make?
Best, average and worst case#
| Case | Input | Time | Why |
|---|---|---|---|
| Best | Already sorted | O(n) | One pass, n - 1 compares, no swaps, stop |
| Average | Random order | O(n²) | Small values near the end move left only one slot per pass, so most passes are still needed |
| Worst | Reverse sorted | O(n²) | n - 1 passes, every compare swaps |
The O(n) best case comes only from the early exit. Without the swapped check, bubble sort always makes all n(n - 1) / 2 compares, even on a sorted list. Extra space is O(1), since it swaps inside the same list, so it is an in-place sort.
Quick check
Quick check: How many swaps does bubble sort make on [4, 3, 2, 1]?
Stable: equal marks keep their order#
A sort is stable if equal values keep the order they started in. Sort these students by marks. Asha and Ravi both have 72, and Asha comes first in roll order.
Bubble sort only swaps neighbours, and only when the left one is strictly bigger. Equal values never pass each other, so it is stable. Write >= instead of > and equal neighbours swap: here, pass 2 would swap Asha and Ravi, and Ravi would end up ahead of Asha.
When would you really use it?#
Almost never in real code. Python's sorted() runs in O(n log n), much faster for big lists. Merge sort, later in this stage, shows how. Bubble sort is fine for a tiny list, or to check in one pass that a list is already sorted. Its real value is the idea of a pass that locks one value into its final place, which comes back in selection sort.
One last check#
Quick check
Quick check: Bubble sort with early exit runs on [1, 2, 3, 5, 4]. How many passes and compares?
Next is selection sort. It also locks one value into place per pass, but it finds that value first and makes at most one swap per pass.