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
| Property | Insertion sort | Quick sort |
|---|---|---|
| Technique | Insert each value into a sorted prefix | Divide and conquer: partition around a pivot |
| Best case time | O(n) | O(n log n) |
| Average case time | O(n²) | O(n log n) |
| Worst case time | O(n²) | O(n²) |
| Extra space | O(1) | O(log n) |
| Compares values with each other | Yes | Yes |
| Stable (equal values keep their order) | Yes | No |
| In place (no extra array) | Yes | Yes |
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).
| Input | Insertion sort | Quick sort |
|---|---|---|
| Mixed | 30 | 31 |
| Already sorted | 12 | 75 |
| Reversed | 48 | 51 |
| Nearly sorted | 14 | 65 |
| Duplicates | 36 | 29 |
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
- Bubble sort vs Selection sort
- Bubble sort vs Insertion sort
- Bubble sort vs Merge sort
- Bubble sort vs Quick sort
- Bubble sort vs Heap sort
- Bubble sort vs Counting sort
- Bubble sort vs Radix sort
- Selection sort vs Insertion sort
- Selection sort vs Merge sort
- Selection sort vs Quick sort
- Selection sort vs Heap sort
- Selection sort vs Counting sort
- Selection sort vs Radix sort
- Insertion sort vs Merge sort
- Insertion sort vs Heap sort
- Insertion sort vs Counting sort
- Insertion sort vs Radix sort
- Merge sort vs Quick sort
- Merge sort vs Heap sort
- Merge sort vs Counting sort
- Merge sort vs Radix sort
- Quick sort vs Heap sort
- Quick sort vs Counting sort
- Quick sort vs Radix sort
- Heap sort vs Counting sort
- Heap sort vs Radix sort
- Counting sort vs Radix sort