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
- Compare each adjacent pair.
- Swap when the left value is larger.
- Shrink the unsorted boundary after every pass.
- 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.