Algo Visualiser
SortingPathfinding

Bubble sort

Repeatedly walks the array swapping neighbours that are out of order until a full pass makes no swaps.

In real life: Mostly a teaching tool: it is the simplest sort to explain and trace by hand.

Live stats

Comparisons
0
of 3,132
Swaps
0
of 1,501
Writes
0
of 0
  1. for i = 0 to n - 2
  2. swapped = false
  3. for j = 0 to n - i - 2
  4. if a[j] > a[j + 1]
  5. swap a[j], a[j + 1]; swapped = true
  6. if not swapped: stop

Press play or → to start.

Time complexity

Best
O(n)
Average
O(n²)
Worst
O(n²)

Legend

  • Comparing
  • Reading a digit (radix)
  • Swap or write (lifted)
  • Sorted
0 / 4,633