Bubble Sort
EasyWalk the array swapping any pair that is out of order. After pass k the k largest values have bubbled to the end.
comparison sortstablein place
TimeO(n^2), best O(n) when already sorted
SpaceO(1)
InputComma separated numbers
5
0
1
1
4
2
2
3
8
4
0
5
2
6
Starting array. Nothing is known to be in place yet.
comparisons0swaps0
1 / 48
for i from 0 to n-1:
swapped = false
for j from 0 to n-i-2:
if a[j] > a[j+1]:
swap(a[j], a[j+1]); swapped = true
if not swapped: break
Why it matters
Stable. Almost never the right answer, but the clearest place to see invariants.
Practise this
Keyboard
space play← → stepr restartctrl K search
More in Sorting
Next visualiser
Selection Sort