Selection Sort vs Insertion Sort

Across our 3 test inputs, Insertion Sort needed 85 comparisons in total versus 135 for Selection Sort. Insertion Sort is stable; Selection Sort is not.

Head-to-head on identical inputs (comparisons)

InputSelection SortInsertion Sort
Random order4530
Nearly sorted4510
Reversed4545

Complexity

Selection SortInsertion Sort
Average timeO(n²)O(n²)
Worst timeO(n²)O(n²)
SpaceO(1)O(1)
StableNoYes

When to use Selection Sort

When memory writes are expensive (minimizes swaps). Otherwise, insertion sort is generally preferred.

When to use Insertion Sort

Small arrays or nearly-sorted arrays. O(n) best case for already-sorted input. Used as a subroutine in Timsort.