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. In the worst case both take O(n²), so the choice comes down to the other differences below.

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)
Stable (equal values keep their order)YesNo
In place (no extra array)YesYes
Needs sorted inputNoNo

When to choose each

Choose insertion sort when

the array is small or nearly sorted.

Avoid it when

the array is large and in random order.

Choose quick sort when

you want a fast in-place sort on typical data.

Avoid it when

you need a worst-case guarantee or a stable sort.

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

See them run

More comparisons