Skip to content
← Back to algorithms

Step 0 / 0 · Comparisons: 0 · Swaps: 0

How this algorithm works

How it works

  1. Start with an unsorted array of numbers.
  2. Compare the first two adjacent elements.
  3. If the left element is greater than the right, swap them.
  4. Move to the next pair and repeat.
  5. After one full pass, the largest element 'bubbles' to the end.
  6. Repeat the process for the remaining unsorted portion.
  7. If a pass completes with no swaps, the array is sorted.

Key concepts

  • Comparison-based sorting
  • Stable sort (preserves relative order of equal elements)
  • In-place (O(1) extra space)
  • Adaptive: best case O(n) with early exit

When to use it

When the input is small or nearly sorted. Rarely used in practice for large datasets due to O(n^2) average complexity.

Did you know

The bubble sort is sometimes called 'sinking sort' because larger elements 'sink' to the bottom.