How bubble sort works
Picture people lined up for a photo, shortest to tallest, where you may only compare two neighbours at a time. If the one on the left is taller, they swap places. Walk along the whole line doing that and the tallest person ends up at the far right. Big values “bubble” to the end, which is where the name comes from.
One walk along the line is called a pass. After the first pass the largest value is settled, after the second pass the two largest are, and so on. That is why each pass can stop one position earlier than the last one: the green bars on the right never need to be looked at again.
The only thing bubble sort needs from your data is that any two values can be compared. It works on numbers, names or dates, in any starting order.
A worked example: sorting 5, 2, 4, 1, 3
Type 5, 2, 4, 1, 3 into the custom array box above and step through it with the arrow keys. This is what you will see at the end of each pass:
Add the rows up and you get 10 comparisons and 7 swaps, exactly what the counters under the bars show when the run finishes.
What i, j and swapped mean
i counts the passes, starting from 0. After pass i, the last i + 1 values are in their final place.
j is the left-hand member of the pair being compared right now, so the pair is always a[j] and a[j+1]. It runs from 0 up to n − 2 − i, which is how the settled values on the right get skipped.
swapped is a yes/no flag that is reset at the start of every pass. If it is still false when the pass ends, nothing was out of order and the loop breaks. Leaving this flag out is the difference between a bubble sort that finishes a sorted list in one pass and one that grinds through all of them.
How many steps does bubble sort take?
Count the comparisons. With 5 values the passes look at 4, 3, 2 and 1 pairs, which is 10. With 10 values it is 9 + 8 + … + 1 = 45. With 20 values it is 190. Doubling the list roughly quadruples the steps.
In general a list of n values needs up to n × (n − 1) ÷ 2 comparisons. The part that matters as lists get long is the n × n, and that growth is what the notation O(n²) is shorthand for. The best case is different: on a sorted list the first pass makes n − 1 comparisons, swaps nothing and stops, which is O(n). Load the Sorted preset and press play to see it.
- Best
- O(n)
- Average
- O(n²)
- Worst
- O(n²)
- Extra memory
- O(1)
- Stable
- Yes
Want to see how fast n² pulls away from n? Measure it in the Big-O Playground →
When bubble sort is the right choice
Honestly: when you are learning. It is the clearest first example of two nested loops working together, and tracing it by hand is a common exam and interview warm-up. It is also fine for a handful of values, or when you only need to check whether a list is already sorted.
For anything bigger, reach for the sort built into your language. It will be many times faster on a thousand values and the gap only widens from there.
Mistakes beginners make
- Running off the end. The inner loop compares
a[j]witha[j+1], sojmust stop atn − 2, notn − 1. - Forgetting the early exit. The sort still works, but a sorted list now costs as much as a shuffled one.
- Re-checking the settled values. Without the
− iin the inner loop's limit, every pass walks the full list and does work that cannot change anything. - Swapping on equal values. Using
>=instead of>wastes swaps and makes the sort unstable.
Bubble sort questions
Why is it called bubble sort?
Because of how the big values move. In every pass the largest unsorted value is swapped again and again until it reaches the end of the list, a bit like an air bubble rising to the surface of a glass of water.
How many passes does bubble sort need?
At most n − 1 passes for a list of n values, because each pass settles at least one value. With the early exit it can need far fewer: a list that is already sorted takes a single pass, and the 5-value example on this page takes four.
Is bubble sort used in real programs?
Rarely. It is mainly a teaching tool, because it is the easiest sort to picture. Its one practical trick is cheap detection: a single pass with no swaps proves that a list is already in order.
Is bubble sort stable?
Yes. It only swaps two neighbours when the left one is strictly bigger, so two equal values never jump over each other and they finish in the order they started in.
What is the difference between bubble sort and selection sort?
Bubble sort fixes the list through many small swaps between neighbours. Selection sort looks through the whole unsorted part first and then makes one long-distance swap per pass. Selection sort therefore swaps much less, but it can never stop early, while bubble sort can.