Skip to content
SimpleScope

Sorting

Insertion sort vs Quick sort

Insertion sort and Quick sort both put an array in order, in different ways. Insertion sort: insert each value into a sorted prefix. Quick sort: divide and conquer: partition around a pivot.

At a glance

PropertyInsertion sortQuick sort
TechniqueInsert each value into a sorted prefixDivide and conquer: partition around a pivot
Best case timeO(n)O(n log n)
Average case timeO(n²)O(n log n)
Worst case timeO(n²)O(n²)
Extra spaceO(1)O(log n)
Compares values with each otherYesYes
Stable (equal values keep their order)YesNo
In place (no extra array)YesYes

When to choose each

Insertion sort: when to choose it

Choose insertion sort for small arrays and for input that is already close to sorted — and note that those are precisely the two cases in which this quick sort is at its worst. Insertion sort is stable, needs no recursion, and its cost falls as the input gets more ordered. Real implementations of quick sort switch to insertion sort below a size threshold for exactly this reason, so the two are complements more than rivals.

Quick sort: when to choose it

Choose quick sort for large arrays whose order you do not control. Its average is O(n log n) against insertion sort's O(n²), it sorts in place, and partitioning does more per comparison than shifting does: each element is placed on the correct side of the pivot once and never reconsidered. Its weaknesses are a worst case that is quadratic, instability, and — with the last element as pivot, as here — a pathological case on sorted input that a median-of-three or random pivot removes.

The same inputs, counted

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

InputInsertion sortQuick sort
Mixed3031
Already sorted1275
Reversed4851
Nearly sorted1465
Duplicates3629

What the numbers show

Insertion sort wins four of the five cases, and the sorted array is not close: 12 operations against 75. That factor of six is the Lomuto partition meeting ascending input, where every comparison succeeds, the pivot ends up at the far end and the recursion strips one element at a time — 21 comparisons and 54 writes. The nearly sorted array is almost as lopsided, 14 against 65, and even on the reversed array, insertion sort's own worst case, it wins 48 against 51.

Quick sort takes the duplicates array, 29 against 36, and the mixed array is a near tie, 31 against 30. The comparison counts are where quick sort looks good: 11 on the mixed array and 13 on the duplicates, against insertion sort's 14 and 17. When its partitions are balanced it needs genuinely fewer comparisons; it is the 20 and 16 writes that bring the totals back level.

Both results are about this n and this pivot rule. Insertion sort's average work is about n²/4, so it doubles twice when the array doubles; quick sort's average is about 1.4 n log₂ n. At n = 7 those are roughly 12 and 28 — insertion sort really is the better algorithm at this size — and at n = 10,000 they are about 25 million and about 190,000. The practical answer is both: quick sort with a hardened pivot on the outside, insertion sort on runs below a few dozen elements.

See them run

More comparisons