Bubble sort
A sorting algorithm that compares neighbors and swaps them if they're backwards; the largest rises to the end like a bubble. O(n²) and stable.
Example
[3, 9, 1] → 3 < 9 untouched, 9 > 1 swapped: [3, 1, 9]. 9 is already at the end.
Test yourself: Bubble sort
Bubble sort
After bubble sort's first pass, the ___ element ends up last.
In [3, 9, 1, 5], 9 keeps swapping with each smaller neighbor until it reaches the end: [3, 1, 5, 9].
Bubble sort
In [2, 5, 1], bubble sort's first pass leaves [2, 1, ___].
2 < 5, untouched; 5 > 1, swapped: [2, 1, 5]. 5 reached the end in 1 swap.
Bubble sort
If a bubble sort pass makes no swaps, the array is already ___.
No pair of neighbors was backwards: [1, 2, 3, 4] is confirmed in 3 comparisons and it stops.
0/3
▶ Play