Selection sort in C repeatedly finds the smallest element in the unsorted part of an array and swaps it into the next position of the sorted part. It runs in O(n²) time in every case, uses O(1) extra space and performs at most n - 1 swaps, which makes it a good teaching algorithm but a poor choice for large inputs.
This article shows a correct C implementation, traces how it works, explains its complexity and compares it with other simple sorts.
The array is split into a sorted prefix on the left and an unsorted suffix on the right. At the start the sorted prefix is empty.
For the array 64 25 12 22 11 the passes look like this:
| Pass | Array after the swap | Sorted prefix |
|---|---|---|
| 1 | 11 25 12 22 64 | 11 |
| 2 | 11 12 25 22 64 | 11 12 |
| 3 | 11 12 22 25 64 | 11 12 22 |
| 4 | 11 12 22 25 64 | 11 12 22 25 |
Compile with gcc -std=c17 -Wall -Wextra -o selection selection.c.
A few details matter in this code:
size_t, the correct type for array sizes. Because size_t is unsigned, the guard n < 2 prevents n - 1 from wrapping around when the array is empty.< rather than <= keeps the first minimum, which avoids needless swaps of equal values.| Case | Comparisons | Swaps | Time |
|---|---|---|---|
| Best (already sorted) | n(n - 1) / 2 | 0 | O(n²) |
| Average | n(n - 1) / 2 | up to n - 1 | O(n²) |
| Worst (reverse order) | n(n - 1) / 2 | up to n - 1 | O(n²) |
The number of comparisons does not depend on the input, because the inner loop always scans the whole unsorted part. The extra space is O(1) since the sort works in place.
Selection sort in its swap form is not stable: swapping the minimum into position can move an equal element past another equal element. For example, sorting 3a 3b 1 produces 1 3b 3a. If you need stability, use a stable algorithm such as insertion sort, merge sort or the library's stable sort.
Choose selection sort when:
For real data, call qsort from <stdlib.h>, which is the standard library routine and is much faster for large arrays:
The comparison idiom (x > y) - (x < y) avoids the overflow that x - y can cause.
| Algorithm | Best | Worst | Swaps or writes | Stable |
|---|---|---|---|---|
| Selection sort | O(n²) | O(n²) | O(n) | No |
| Insertion sort | O(n) | O(n²) | O(n²) | Yes |
| Bubble sort | O(n) with early exit | O(n²) | O(n²) | Yes |
It has no early exit. The inner loop must scan the whole unsorted part to be sure it found the minimum, so the comparison count is the same for any input.
Yes. It only needs a few local variables and swaps elements inside the array, so extra space is O(1).
Change the comparison to arr[j] > arr[min_index] and, ideally, rename the variable to max_index.
Usually yes, because it does far fewer swaps, though both are O(n²) and both are slow on large arrays.
Selection sort picks the minimum of the unsorted part and swaps it into place, one position per pass. It is simple, in-place and uses at most n - 1 swaps, but it is always O(n²) and not stable. Use it for learning or tiny arrays and use qsort or a better algorithm in production code.