Sorting
Selection sort vs Insertion sort
Selection sort and Insertion sort both put an array in order, in different ways. Selection sort: repeatedly pick the smallest remaining value. Insertion sort: insert each value into a sorted prefix.
At a glance
| Property | Selection sort | Insertion sort |
|---|---|---|
| Technique | Repeatedly pick the smallest remaining value | Insert each value into a sorted prefix |
| Best case time | O(n²) | O(n) |
| Average case time | O(n²) | O(n²) |
| Worst case time | O(n²) | O(n²) |
| Extra space | O(1) | O(1) |
| Compares values with each other | Yes | Yes |
| Stable (equal values keep their order) | No | Yes |
| In place (no extra array) | Yes | Yes |
When to choose each
Selection sort: when to choose it
Choose selection sort when a write is far more expensive than a comparison. It makes at most n − 1 swaps, because each pass finds the minimum and moves it exactly once into its final place; insertion sort shifts every value that has to make room, so a single element arriving at the front of a long sorted prefix costs a shift per element it passes. If your elements are large records, or the array lives on storage where writing is slow or wears out, that bound is the argument.
Insertion sort: when to choose it
Choose insertion sort when comparisons dominate, when the data is partly ordered, or when you need stability. Its comparison count falls with the amount of order already present, so it has an O(n) best case where selection sort is O(n²) in every case. It is also stable and online: it keeps a correct sorted prefix after every step, so you can feed it elements as they arrive. It is the small-array sort real libraries use, and selection sort is not.
The same inputs, counted
These numbers come from running both real implementations: operations (comparisons plus writes, a swap counting as two writes).
| Input | Selection sort | Insertion sort |
|---|---|---|
| Mixed | 29 | 30 |
| Already sorted | 21 | 12 |
| Reversed | 27 | 48 |
| Nearly sorted | 23 | 14 |
| Duplicates | 31 | 36 |
What the numbers show
Split the counts into comparisons and writes and the two algorithms separate cleanly. Insertion sort always makes fewer comparisons or the same: 14 against 21 on the mixed array, 6 against 21 on the sorted one, 7 against 21 on the nearly sorted one, 17 against 21 on the duplicates, and 21 each on the reversed array. Selection sort always makes far fewer writes: 8 against 16 on the mixed array, 0 against 6 on the sorted one, and 6 against 27 on the reversed one.
Which one wins the total therefore depends on the input. Pre-ordered input goes to insertion sort by a wide margin — 12 operations against 21 on the already-sorted array, 14 against 23 on the nearly sorted one — because its comparison count collapses and selection sort's never does. Disordered input goes to selection sort: 27 against 48 on the reversed array, 31 against 36 on the duplicates, and the mixed array by a single operation, 29 against 30.
The reversed array is the clearest case. Both do 21 comparisons, the maximum for seven elements, so the entire difference is memory traffic: 6 writes against 27. Every value insertion sort inserts has to be shifted past the whole prefix. Note also that selection sort's comparison count is a constant 21 on all five inputs — n(n − 1)/2, with no dependence on the data — which is why its "best case" row reads O(n²) while insertion sort's reads O(n).
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 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 Quick 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