SortLab

Foundations · Beginner

Bubble Sort

Sweep through neighboring pairs, swapping any pair that is out of order.

Best
O(n)
Average
O(n²)
Worst
O(n²)
Space
O(1)
Stability
Stable
Memory model
In-place

How it builds order

  1. Compare each adjacent pair.
  2. Swap when the left value is larger.
  3. Shrink the unsorted boundary after every pass.
  4. Stop early when an entire pass makes no swaps.

Why it works

Every comparison moves the larger of two neighbors rightward. A swap-free pass proves that no inversion remains.

Watch for: After each complete pass, one more value is locked into its final position.

The interactive version of this page adds the full trace, synchronized pseudocode, practice decisions, input experiments, and mastery review.

Compare related strategies