How selection sort works
Imagine sorting a pile of coins by value. You look over every coin that is left, find the smallest, and put it at the front. Then you do the same with the rest. That is selection sort: each pass selects the smallest remaining value and drops it into the next free slot.
The list is always split in two. On the left is the sorted part, which starts empty and grows by one value per pass. On the right is everything still waiting. Nothing in the sorted part ever moves again, because every value in it is smaller than everything to its right.
Like bubble sort, all it needs is a way to compare two values. The starting order makes no difference to how much work it does, and that turns out to be both its weakness and its most interesting feature.
A worked example: sorting 5, 2, 4, 1, 3
Type 5, 2, 4, 1, 3 into the custom array box above and step through it. At the end of each pass the list looks like this:
That is 10 comparisons but only 3 swaps. Bubble sort needs 7 swaps for the same list. Watch the Swaps counter to see the difference.
What i, j and min mean
i is the slot being filled. Everything to the left of i is already sorted.
j is the scanner. It starts at i + 1 and walks to the end of the list, looking at one value per step.
min is the important one. It holds the position of the smallest value seen so far in this pass, not the value itself. It starts out equal to i and changes whenever a[j] is smaller than a[min]. This is the first loop many beginners meet that has to remember something from one step to the next.
How many steps does selection sort take?
The first pass compares the candidate with all n − 1 other values, the second pass with n − 2, and so on down to 1. For 5 values that is 4 + 3 + 2 + 1 = 10. For 10 values it is 45, and for 20 it is 190.
The total is always exactly n × (n − 1) ÷ 2. It grows with n × n, which is written O(n²), and there is no lucky case: selection sort has no way to notice that the list is already in order, so best, average and worst are all the same.
- Best
- O(n²)
- Average
- O(n²)
- Worst
- O(n²)
- Extra memory
- O(1)
- Stable
- No
New to this notation? Start with the Big-O Playground →
When selection sort is the right choice
When moving data costs far more than looking at it. Writing to some kinds of memory is slow or wears the hardware out, and selection sort never makes more than n − 1 swaps. It is also a good fit for very small lists and for situations where you want the running time to be the same every time.
It also teaches an idea you will reuse everywhere: finding the smallest (or largest) value by scanning with a running best. That single loop is the heart of selection sort.
Mistakes beginners make
- Storing the value instead of its position. If
minholds the smallest value, you know what to move but not where it is. Keep the index. - Swapping inside the inner loop. Swapping every time a smaller value turns up still sorts the list, but it throws away the whole point of the algorithm. Swap once, after the scan.
- Starting the scan in the wrong place.
jbegins ati + 1. Starting at 0 drags sorted values back into play. - Expecting it to be stable. If the order of equal values matters, use insertion sort instead.
Selection sort questions
Why is selection sort always O(n²), even on a sorted list?
Because it cannot know a value is the smallest without looking at every value that is left. It makes the same n × (n − 1) ÷ 2 comparisons whether the list is shuffled, reversed or already in order. Try the Sorted preset: the Comparisons counter ends on the same number as it does for a random list.
Why is selection sort not stable?
Its swap moves a value a long way in one jump, and that jump can carry it past an equal value. Sort 4a, 4b, 1 and the first pass swaps 1 with 4a, leaving 1, 4b, 4a. The two 4s have traded places.
How many swaps does selection sort make?
At most n − 1, one per pass, and fewer when a value is already where it belongs. That is the fewest of the three simple sorts. Bubble sort can need one swap for every out-of-order pair.
Selection sort or insertion sort: which is better?
Insertion sort, most of the time. It finishes early on lists that are nearly sorted, and it is stable. Selection sort wins only when writing a value is far more expensive than comparing two values.
Does selection sort need extra memory?
No. It sorts inside the list it was given and needs only a few variables (i, j and min), so its extra memory is O(1) however long the list is.