level 3 · Sorting
Bubble Sort
Compare neighbours, swap if they're wrong. Repeat until calm.
what is it
Start here
Bubble sort only ever looks at two neighbours at a time. If they're in the wrong order, it swaps them. Then it shuffles one step right and does it again.
Do that all the way along the list, and something quietly useful happens: the biggest value gets dragged to the far end, like a bubble rising to the surface. It is now in its final place forever. Do it again and the second biggest lands. And so on.
It is the easiest sort to understand and one of the slowest to run — which is exactly why it's worth watching once. You can see the wasted effort: it compares the same pairs again and again, learning very little each pass.
real-life analogy
Picture it
You look at two books side by side. Out of order? Swap them. Shuffle right, look at the next two. You never see the whole shelf at once — you only ever fix the pair in front of your nose. It works, eventually. Nobody would call it clever.
interactive visualization
Watch it run
Type numbers separated by spaces. Try one that's already sorted.
- 8
- 3
- 5
- 1
- 9
- 2
- 7
Bubble sort compares two neighbours at a time. The biggest value keeps floating to the end, like a bubble rising to the surface.
step 01/38
- comparing
- moving
- in final place
space · play ← → · step
| 1 | function bubbleSort(a) { |
| 2 | for (let i = 0; i < a.length - 1; i++) { |
| 3 | let swapped = false; |
| 4 | for (let j = 0; j < a.length - 1 - i; j++) { |
| 5 | if (a[j] > a[j + 1]) { |
| 6 | [a[j], a[j + 1]] = [a[j + 1], a[j]]; |
| 7 | swapped = true; |
| 8 | } |
| 9 | } |
| 10 | if (!swapped) break; |
| 11 | } |
| 12 | return a; |
| 13 | } |
variables right now
Nothing in play yet — press play.
the dry run · every step, in words
38 stepscomplexity
What it costs
- best case
- O(n)
- average
- O(n²)
- worst case
- O(n²)
- extra memory
- O(1)
Roughly n²/2 comparisons in the worst case: 10 items cost about 45 comparisons, 100 items cost about 5,000. The best case is O(n) only because of the early-exit check — one clean pass with no swaps proves the list is already sorted.
- O(1)
- O(log n)
- O(n)
- O(n log n)
- O(n²) · this one
common mistakes
Common traps
Leaving out the 'did anything swap?' check.
Without it, an already-sorted list still costs the full n² passes. That one boolean turns the best case from O(n²) into O(n).
Looping j all the way to the end on every pass.
After pass i, the last i items are already final. Use `j < n - 1 - i` and stop re-checking values you've proven are done.
Reaching for bubble sort in real code.
Don't. Every language ships a sort that is dramatically faster. Bubble sort is for understanding what sorting *is*, not for doing it.
quiz
Check yourself
Three questions. Get them all right to finish the lesson.
+50 XP01After one full pass of bubble sort, what do you know for certain?
02You run bubble sort on a list that is already sorted. With the early-exit check, what happens?
03Roughly how many comparisons does bubble sort need for 100 items in the worst case?
practice
Solve it on LeetCode
You've seen it run — now write it yourself. These are real LeetCode problems that use exactly this idea, from gentlest to toughest.