Insertion Sort vs Quick Sort

Across our 3 test inputs, Insertion Sort needed 85 comparisons in total versus 106 for Quick Sort. On average Insertion Sort runs in O(n²) time and Quick Sort in O(n log n). Insertion Sort is stable; Quick Sort is not.

Head-to-head on identical inputs (comparisons)

InputInsertion SortQuick Sort
Random order3022
Nearly sorted1039
Reversed4545

Complexity

Insertion SortQuick Sort
Average timeO(n²)O(n log n)
Worst timeO(n²)O(n²)
SpaceO(1)O(log n)
StableYesNo

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.

When to use Quick Sort

General-purpose sorting. Fast in practice due to excellent cache locality. Default algorithm used in many language libraries (often hybridised as Introsort).