Skip to content
SimpleScope

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

PropertySelection sortQuick sort
TechniqueRepeatedly pick the smallest remaining valueDivide 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)NoNo
In place (no extra array)YesYes

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).

InputSelection sortQuick sort
Mixed2931
Already sorted2175
Reversed2751
Nearly sorted2365
Duplicates3129

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