Five sorts really run on your sacks of grain: every comparison and swap from the run, and sacks of equal weight show which sort is stable. An interactive…
Why it matters
Sorting is the doorway to algorithmic thinking: counting comparisons and swaps makes "O(n²) is slow" something you feel, and sacks of equal weight show what stable means, which matters as soon as you sort data in layers (orders by date, then by name).
The key idea
Every sort only compares and moves; what differs is which pairs and how far
Common mistakes
Using bubble sort in production O(n²) does not scale: 12 reversed sacks already take 66 comparisons, 1,000 sacks almost 500,000. Use the built-in arr.sort((a, b) => a - b) (O(n log n)); learn bubble to understand.
[10, 2, 1].sort() → [1, 10, 2] With no comparator, sort turns every value into text and compares character by character. For numbers give (a, b) => a - b; for objects compare a field, like .kg here.
Thinking Big-O is seconds Big-O is how the number of steps grows with n, not time. O(n²) is fine for small n, fatal for large n.
Thinking every sort keeps equal values in order Selection and quick are not stable: two 12 kg sacks can swap. When arrival order matters, use a stable sort (merge, insertion, or the built-in sort).
Try it in this playground
Run bubble to the end The heaviest is carried to the right end each pass; green sacks are in place.
Selection: count its swaps At most n − 1 swaps, but its comparisons never drop.
Insertion on a nearly sorted row Comparisons far below n²: only the one sliding in works hard.
Merge and its bench A stretch is lifted onto the bench, then written back in the new order.