learn/Algorithms/Bubble Sort
ƒ(x) SortingCore interactive

Bubble Sort

Adjacent swaps bubble the largest to the end.

bubble_sort
step 1 / 54
44
8
18
20
88
94
64
34
50
94
94

Unsorted input. Compare each adjacent pair.

comparingmovingsorted
speed1×

How it works

Bubble sort repeatedly walks the list, swapping any out-of-order neighbors. After each pass the next-largest element 'bubbles' to its final spot. Simple and stable, but O(n²) — a teaching tool more than a production sort.

Mental models

  • Each pass guarantees one more element reaches its final position.
  • A 'no swaps this pass' flag lets it exit early on sorted input.

Complexity

Best
O(n)already sorted, with early exit
Average
O(n²)
Worst
O(n²)
Space
O(1)in-place