designwithfocus

Programming basics

What Is Bubble Sort? Pairwise Swaps Until It Settles

By Design With Focus Team 6 min read
Five numbered boxes swapping places across three passes

Short answer

Bubble sort walks a list in neighbor pairs, swaps any pair that is out of order, and repeats until a full pass swaps nothing. NIST also calls it sinking sort and exchange sort, and classifies it as an in-place stable sort. On arbitrary data, NIST states the complexity as O(n squared). The walk-through below uses the list 5, 1, 4, 2, 8.

Last verified against the sources listed below.

Key takeaways

  • Compare each adjacent pair, swap if needed, and repeat until a pass does no swaps.
  • NIST calls the complexity O(n squared) for arbitrary data, and lower if the list is nearly ordered.
  • Wikipedia's worked list 5, 1, 4, 2, 8 needs three passes, and the last pass does not swap.
  • The largest value can move many steps in one pass. A small value moves one step per pass.
  • Princeton's implementation is stable, uses a constant amount of extra memory, and stops early if a pass swaps nothing.

What is bubble sort? It is a sort that compares neighbors, swaps them when they are out of order, and keeps passing through the list until a pass makes no swaps.

Programming paradigms are a wider topic. Bubble sort is one small algorithm inside an imperative loop, not a paradigm of its own.

What is the definition?

NIST defines it as pairwise comparison and swapping, repeated until a clean pass.

The Dictionary of Algorithms and Data Structures, edited by Paul E. Black and cited there as updated 7 April 2023, says to sort by comparing each adjacent pair in turn, swapping if necessary, and repeating the pass until no swaps are done. It also calls the method sinking sort and exchange sort. It classifies bubble sort as an in-place sort and a stable sort.

Wikipedia’s article describes the same motion. The algorithm steps through the list, compares the current element with the next, and swaps if needed. Passes repeat until a pass swaps nothing, which means the list is sorted. The name comes from larger elements moving toward one end.

A Princeton course note puts the same idea in two nested loops. The inner index walks neighbors. The outer loop repeats the walk. After the first pass the largest element is at the end, so a later pass can stop before that end.

How do the swaps go for 5, 1, 4, 2, 8?

Three passes sort this list. The last pass only checks.

Wikipedia uses this exact list and the same swaps. Start at 5, 1, 4, 2, 8. Compare toward the right. Swap when the left value is greater.

Pass Comparison Swap? List after the step
1 5 and 1 Yes, 5 is greater 1, 5, 4, 2, 8
1 5 and 4 Yes 1, 4, 5, 2, 8
1 5 and 2 Yes 1, 4, 2, 5, 8
1 5 and 8 No 1, 4, 2, 5, 8
2 1 and 4 No 1, 4, 2, 5, 8
2 4 and 2 Yes 1, 2, 4, 5, 8
2 4 and 5 No 1, 2, 4, 5, 8
2 5 and 8 No 1, 2, 4, 5, 8
3 1 and 2, then 2 and 4, then 4 and 5, then 5 and 8 No swaps 1, 2, 4, 5, 8

Four swaps happen in total. Three of them are in the first pass, and they walk 5 toward the right until 8 stops it. The second pass has one swap, 4 with 2.

The third pass is the proof that nothing is left to swap. Wikipedia says the algorithm needs that extra clean pass before it knows it is finished.

NIST notes an improvement: at least one item is in its final place after each pass, so the next pass can stop one position earlier. The table above still checks the whole list, which matches the simple definition. The shortened pass is optional, not a different algorithm.

What do the published bounds say?

NIST, Wikipedia, and Princeton each print a bound. They are not a timed bake-off.

NIST says complexity is O(n squared) for arbitrary data, and that it approaches Θ(n) if the list is nearly in order at the start. Wikipedia says the worst case and the average case are O(n squared) comparisons and O(n squared) swaps. It says the best case is O(n) comparisons and O(1) swaps, which is the already-sorted list that still needs one clean pass of neighbor checks.

Princeton’s Bubble.java comment says that implementation makes about one half n squared compares and exchanges in the worst case, so it is not suitable for large arbitrary arrays. It also says the sort is stable and uses O(1) extra memory.

The cheatsheet marks bubble sort in place and stable, with best case order n, average one half n squared, and worst one half n squared. The remark there is that it is rarely useful because insertion sort dominates it.

A heap is a different structure, used by other sorts. It is not required for these neighbor swaps.

The Princeton course page also shows why the nested loops are quadratic. Each pass walks the array, and you repeat the pass. The upper bound of the inner loop can shrink, because the end is already in order. Shrinking the loop does not change the NIST statement about arbitrary data.

Where does the plain version fall short?

Large values move quickly, and small values at the end move slowly.

Wikipedia calls those rabbits and turtles. The largest item can win every comparison and reach the end on the first pass, which is what 5 does above until it meets 8. The smallest item, if it starts at the end, moves only one slot per pass.

Bidirectional bubble sort, which NIST describes separately, alternates direction so an item can move toward either end. The worked example is the one-direction version.

Wikipedia says the method is used mainly as a teaching tool, and that library sorts in languages such as Python and Java use other algorithms. That is a statement about common practice on that page, not a benchmark run for this guide.

What does the code amount to?

The algorithm is a short loop. A demo of it is just that code.

Princeton’s course snippet compares a[j+1] with a[j] and swaps through a temporary variable. The Java class does the same job from the other end of the array, counts exchanges, and breaks when a pass exchanges nothing. Both match the NIST stop rule.

The SVG to JSX tool generates component code from a drawing. A sorting demo is the same kind of artifact: a small piece of code you can read, not a service that sorts your production data.

Frequently asked questions

Why does the third pass exist if the list is already sorted?

Wikipedia's walk-through of 5, 1, 4, 2, 8 says the array is sorted after the second pass, and the algorithm still does not know that. It stops only when a whole pass makes no swaps. The third pass compares neighbors, swaps nothing, and then stops. That empty pass is the test. NIST also notes that one item reaches its final place on each pass, so a later pass can check a shorter range.

What does O(n squared) refer to here?

NIST states that complexity for arbitrary data. Wikipedia states a worst case and an average case of O(n squared) comparisons and swaps, and a best case of O(n) comparisons with O(1) swaps. Princeton's Bubble class comment says that implementation makes about one half n squared compares and exchanges in the worst case. The n is the length of the list. These are the bounds those pages print.

Is bubble sort the same as insertion sort?

No. Both can take quadratic time, and Wikipedia says bubble sort is rarely used in practice because other quadratic sorts, including insertion sort, generally run faster. Princeton's cheatsheet marks bubble sort stable and in place, and remarks that insertion sort dominates it. NIST lists bubble sort as its own entry. Use it to see pairwise swaps, not as a synonym for every simple sort.

Does the sort need extra space for another list?

NIST classifies it as an in-place sort. Wikipedia lists worst-case space as O(n) total and O(1) auxiliary, which means the extra memory beyond the list itself stays constant. Princeton's comment says the algorithm uses O(1) extra memory. The swaps happen inside the same array. You do not allocate a second copy of the list to hold the sorted result.

Which way do values move?

Wikipedia describes rabbits and turtles. A large value can swap on every comparison, so it can travel to the end in one pass. A small value at the end moves only one position per pass, so it can take n minus 1 passes to reach the front. NIST notes that bidirectional bubble sort moves an item toward either end. The plain version in the worked example only swaps a larger neighbor toward the right.

Sources