AlgoScope

Bubble Sort

Swap neighbours that are out of order until each pass floats one value home.

Sorting, algorithm, beginner
Comparing 5 at index 0 with 1 at index 1. 5 is larger, so they swap places.10514223843576jj+15 > 1
Comparing 5 at index 0 with 1 at index 1. 5 is larger, so they swap places.10514223843576jj+15 > 1

Bubble Sort: Seven values

Decision, step 2 of 24

5 is larger than 1, so they swap.

Cost

Best
O(n)
Average
O(n^2)
Worst
O(n^2)
Space
O(1)

Best case O(n) requires the early-exit check on an already sorted array.

Properties

  • Yes: Comparison based
  • Yes: Stable
  • Yes: In place
  • Yes: Adaptive
  • No: Online

Before this

Variants

  • Early ExitStop when a pass makes no swaps.

Leads to

Topics that need this one first.