A+DS

Bubble Sort

The simplest sort: let big values bubble up!

Imagine bubbles rising in water. Bigger bubbles rise faster. Bubble Sort repeatedly walks through the list, compares neighbors, and swaps them if they're in the wrong order. After each pass, the largest unsorted element "bubbles" to its correct position at the end.

  [  ?  ?  ?  ?  ?  ]     unsorted
   ↑  ↑
  compare neighbors

  [ smaller | bigger ]    swap if needed

  [  ?  ?  ?  ?  ✔  ]     biggest bubbles to end
  [  ?  ?  ?  ✔  ✔  ]     repeat...
  [  ✔  ✔  ✔  ✔  ✔  ]     done!

How it works

  1. 1Walk left to right comparing adjacent pairs
  2. 2If left > right, swap them
  3. 3After each full pass, one more element is in its final place
  4. 4Repeat until no swaps are needed

Loading...