Insertion sort
easy 14 minInsertion sort builds a sorted prefix one value at a time: it takes the next value, shifts every bigger value in the prefix one slot right, and drops the value into the gap.
Insertion sort with a key function and counters
Read it, run it, change a line and run it again.
Loading editor…
Sorts in place and is stable because the loop stops at equal keys. The counters show the cost: shifts always equal the number of inversions.