Bubble sort is the first sorting algorithm most people learn. It is slow, so you would rarely use it for real — but it is the clearest way to see how sorting works. You compare two neighbors, swap them if they are in the wrong order, and repeat. Do that enough times and the whole list ends up sorted.
Step through the animation on the right. The highlighted line of code shows exactly what the algorithm is doing at each comparison and swap — watch the biggest number "bubble" to the end.
The idea in one sentence
Walk through the array from left to right. Whenever a number is bigger than the one just after it, swap them. The biggest number keeps getting pushed right until it reaches the end.
After one full pass, the largest value is guaranteed to be in the last spot. After the second pass, the two largest are in place — and so on. That is why each pass can stop a little earlier than the last.
Walk through it
Press Play on the right, or step with Next / Back. Notice three things:
- The two cells being compared turn blue.
- When they are out of order, they turn red and slide past each other — that is the swap.
- At the end of each pass, the value that reached its final spot turns green and locks in place.
The code panel highlights the matching line: the if when comparing, the swap line when swapping.
The code, line by line
def bubble_sort(a):
n = len(a)
for i in range(n): # one pass per i
for j in range(n - i - 1): # walk the unsorted part
if a[j] > a[j + 1]: # neighbors out of order?
a[j], a[j + 1] = a[j + 1], a[j] # swap
return a- The outer loop (
i) counts the passes. - The inner loop (
j) walks the part that is not sorted yet. It stops atn - i - 1because the lastivalues are already in place — no need to check them again. - Line 5 is the comparison you see highlighted in blue.
- Line 6 is the swap. In Python you can swap two values in one line with
a, b = b, a.
Complexity
| Case | Time | Notes |
|---|---|---|
| Best | O(n) (moderate) | already sorted (with an early-exit check) |
| Average | O(n²) (slow) | |
| Worst | O(n²) (slow) | reverse-sorted input |
O(1) (fast)Why O(n²)? There are about n passes, and each pass compares about n pairs. So the work grows with n × n. The space is O(1) because it sorts in place — it only swaps inside the original array and never makes a copy.
When would you use it?
Almost never in real code — O(n²) is too slow for big inputs, and every language ships a fast
built-in sort. But bubble sort is the perfect way to explain two ideas in an interview: an
in-place swap, and a stable sort (equal values keep their original order). Reach for merge
sort or quick sort when you actually need speed.
A common mistake: forgetting that each pass can stop earlier. After i passes, the last i items
are already sorted, so the inner loop should run to n - i - 1, not all the way to n - 1.
Checking them again is wasted work.
Practice
After the very first pass over [5, 1, 4, 2], which value is guaranteed to be in its final position?
1. What does one pass of bubble sort guarantee?
2. Why is bubble sort O(n²) in the worst case?
3. What does 'in place' mean here?
4. In Python, what does `a[j], a[j+1] = a[j+1], a[j]` do?