Skip to content
SimpleScope

Sorting

Selection sort vs Insertion sort

Selection sort and Insertion sort both put an array in order, in different ways. Selection sort: repeatedly pick the smallest remaining value. Insertion sort: insert each value into a sorted prefix.

At a glance

PropertySelection sortInsertion sort
TechniqueRepeatedly pick the smallest remaining valueInsert each value into a sorted prefix
Best case timeO(n²)O(n)
Average case timeO(n²)O(n²)
Worst case timeO(n²)O(n²)
Extra spaceO(1)O(1)
Compares values with each otherYesYes
Stable (equal values keep their order)NoYes
In place (no extra array)YesYes

When to choose each

Selection sort: when to choose it

Choose selection sort when a write is far more expensive than a comparison. It makes at most n − 1 swaps, because each pass finds the minimum and moves it exactly once into its final place; insertion sort shifts every value that has to make room, so a single element arriving at the front of a long sorted prefix costs a shift per element it passes. If your elements are large records, or the array lives on storage where writing is slow or wears out, that bound is the argument.

Insertion sort: when to choose it

Choose insertion sort when comparisons dominate, when the data is partly ordered, or when you need stability. Its comparison count falls with the amount of order already present, so it has an O(n) best case where selection sort is O(n²) in every case. It is also stable and online: it keeps a correct sorted prefix after every step, so you can feed it elements as they arrive. It is the small-array sort real libraries use, and selection sort is not.

The same inputs, counted

These numbers come from running both real implementations: operations (comparisons plus writes, a swap counting as two writes).

InputSelection sortInsertion sort
Mixed2930
Already sorted2112
Reversed2748
Nearly sorted2314
Duplicates3136

What the numbers show

Split the counts into comparisons and writes and the two algorithms separate cleanly. Insertion sort always makes fewer comparisons or the same: 14 against 21 on the mixed array, 6 against 21 on the sorted one, 7 against 21 on the nearly sorted one, 17 against 21 on the duplicates, and 21 each on the reversed array. Selection sort always makes far fewer writes: 8 against 16 on the mixed array, 0 against 6 on the sorted one, and 6 against 27 on the reversed one.

Which one wins the total therefore depends on the input. Pre-ordered input goes to insertion sort by a wide margin — 12 operations against 21 on the already-sorted array, 14 against 23 on the nearly sorted one — because its comparison count collapses and selection sort's never does. Disordered input goes to selection sort: 27 against 48 on the reversed array, 31 against 36 on the duplicates, and the mixed array by a single operation, 29 against 30.

The reversed array is the clearest case. Both do 21 comparisons, the maximum for seven elements, so the entire difference is memory traffic: 6 writes against 27. Every value insertion sort inserts has to be shifted past the whole prefix. Note also that selection sort's comparison count is a constant 21 on all five inputs — n(n − 1)/2, with no dependence on the data — which is why its "best case" row reads O(n²) while insertion sort's reads O(n).

See them run

More comparisons