Sorting
Selection sort vs Quick sort
Selection sort and Quick sort both put an array in order, in different ways. Selection sort: repeatedly pick the smallest remaining value. Quick sort: divide and conquer: partition around a pivot.
At a glance
| Property | Selection sort | Quick sort |
|---|---|---|
| Technique | Repeatedly pick the smallest remaining value | 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) | No | No |
| In place (no extra array) | Yes | Yes |
When to choose each
Selection sort: when to choose it
Choose selection sort when the array is tiny or when you need the cost to be the same every time. Its totals move hardly at all across inputs, and its write count is bounded by n − 1 swaps. Quick sort's cost is only predictable on average: this implementation partitions around the last element, so an ascending array drives it straight into its O(n²) worst case with a large constant. If you cannot control what the input looks like and the array is small, the boring algorithm is the safe one.
Quick sort: when to choose it
Choose quick sort for large arrays in unknown order. Its average is O(n log n) against selection sort's unconditional O(n²), and it achieves that in place with only O(log n) stack for the recursion, so it beats selection sort on both time and memory at scale. Each comparison in a partition is worth more than a comparison in a minimum scan, because it permanently assigns an element to one side of the pivot. Harden the pivot choice before shipping it.
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 | Quick sort |
|---|---|---|
| Mixed | 29 | 31 |
| Already sorted | 21 | 75 |
| Reversed | 27 | 51 |
| Nearly sorted | 23 | 65 |
| Duplicates | 31 | 29 |
What the numbers show
Selection sort wins four of the five cases here, which is not what the complexity rows would lead you to expect. The sorted array is the extreme: 21 operations against 75, because the Lomuto partition with the last element as pivot finds every element less than or equal to it, puts the pivot at the end, and recurses on all n − 1 remaining elements — 21 comparisons and 54 writes. The nearly sorted array is nearly as bad, 23 against 65, and the reversed array goes 27 against 51.
Quick sort wins the duplicates array, 29 against 31, and the mixed array is almost a tie at 31 against 29. On the mixed array quick sort makes only 11 comparisons, half of selection sort's fixed 21, so when the partitions are balanced the comparison advantage is real and visible even at n = 7. Its 20 writes are what close the gap back up.
Both of these are small-n artefacts of opposite kinds. Selection sort's 21 comparisons are n(n − 1)/2 and will grow quadratically whatever the data; quick sort's 75 on sorted input is a pivot-selection flaw that median-of-three or a random pivot removes, after which its cost on sorted input drops to the same O(n log n) as everything else. At any size worth measuring, choose quick sort and fix the pivot.
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 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