Bubble, selection and insertion are the O(n²) classics — simple enough to trace on paper and honest enough to show what sorting really costs.
1 Bubble
Compare neighbours, swap if out of order. Each pass locks the biggest remaining value into place at the end.
2 Selection
Scan for the smallest value, swap it to the front. Always exactly n − 1 swaps, no matter the input.
3 Insertion
Grow a sorted zone on the left, shift each new value into its slot. Nearly-sorted input makes it fly.
4 Best vs worst
Insertion on sorted input: zero shifts, O(n). All three on a reversed list: O(n²) — n(n−1)/2 comparisons.
5 The exit flag
A bubble pass with zero swaps proves the list is sorted — the early-exit trick turns 5 passes into 4.
Three first sorts
Bubble1Compare neighbours, swap if out of order. Each pass locks the biggest remaining value into place at the end.
Selection2Scan for the smallest value, swap it to the front. Always exactly n − 1 swaps, no matter the input. Insertion3Grow a sorted zone on the left, shift each new value into its slot. Nearly-sorted input makes it fly. Best vs worst4Insertion on sorted input: zero shifts, O(n). All three on a reversed list: O(n²) — n(n−1)/2 comparisons. The exit flag5A bubble pass with zero swaps proves the list is sorted — the early-exit trick turns 5 passes into 4. 5 building blocks make up Three first sorts
[6, 3, 8, 2, 5] — watch the biggest value bubble to the end, pass by pass.
1 / 13nums = [6, 3, 8, 2, 5]. Bubble sort pushes the largest value to the end, one pass at a time.
Two nested loops and one flag — the early exit lives in the swapped check.
process decision input / output start / end Full bubble sort with the early-exit flag, then selection sort’s four guaranteed swaps.
moduleThe unsorted starter list.
Memory
nums= [6, 3, 8, 2, 5]
Console
output appears as the program runs…
[12, 4, 9, 15, 6] — grow the sorted zone from the left, shifting newcomers into place.
1 / 7Insertion sort grows a sorted zone from the left. A single bar — 12 — is trivially sorted.
Exactly one bubble pass — where does the big value land?
Your predicted output — one line per console line
Hint: Each compare touches neighbours: 7 > 2 swaps, 7 < 9 does not, 9 > 4 swaps. One pass can only push the largest value one slot short of the end.
Click a term, then click its meaning to lock the pair.
8 conceptsTap a card to expand ?bubble passdrops into place
?swapped flagdrops into place
?selection sortdrops into place
?insertion sortdrops into place
?O(n²) worstdrops into place
?O(n) bestdrops into place
?a, b = b, adrops into place
?stabledrops into place
01Bubble sort: compare neighbours, swap if out of order — the biggest value bubbles to the end each pass.
02The swapped flag turns bubble into an early-exit sort: one clean pass is proof of sorted.
03Selection sort always makes n − 1 swaps — cheap writes, but O(n²) comparisons no matter the input.
04Insertion sort shines on nearly-sorted data: best case O(n), worst case O(n²).
05All three are perfect for small lists; big lists wait for merge and quick sort.
06a, b = b, a swaps without a temporary — idiomatic Python.