Skip to content
StackDecode
SortingAnimated

Bubble Sort

Swap adjacent out-of-order pairs until none remain.

Time

O(n²)

Extra space

O(1)

Use it for

Teaching; nearly-sorted tiny lists.

How it works

  1. Compare each pair of neighbours from the left.
  2. If a pair is out of order, swap it.
  3. After one full pass the largest value has "bubbled" to the end.
  4. Repeat on the shorter unsorted part; stop when a pass makes no swaps.

Watch it run

Loading animation…