Selection sort finds the smallest element in the unsorted part of the array and swaps it into the next position. Then it does the same for the rest: second smallest into position 1, third smallest into position 2, until the array is sorted.
It always does about n²/2 comparisons, whatever the input looks like, but it never makes more than n − 1 swaps. That makes it a reasonable choice only when writing an element is much more expensive than comparing two.
Usage
int[] numbers = { 1, 5, 4, 11, 20, 8, 2, 98, 90, 16 };
SelectionSort.Sort(numbers);
Console.WriteLine(string.Join(", ", numbers));
// 1, 2, 4, 5, 8, 11, 16, 20, 90, 98
The method is generic, so it works for any type that implements IComparable<T>.
Implementation
public static class SelectionSort
{
public static void Sort<T>(T[] items) where T : IComparable<T>
=> Sort(items.AsSpan());
public static void Sort<T>(Span<T> items) where T : IComparable<T>
{
for (int i = 0; i < items.Length - 1; i++)
{
// Find the smallest element in items[i..].
int min = i;
for (int j = i + 1; j < items.Length; j++)
{
if (items[j].CompareTo(items[min]) < 0)
min = j;
}
// One swap per position, at most.
if (min != i)
(items[i], items[min]) = (items[min], items[i]);
}
}
}
How it works
The array is split into a sorted prefix items[..i] and an unsorted rest. The inner loop only reads: it scans the rest and remembers the index of the smallest value. The swap happens once, after the scan, and moves that value to position i. The outer loop stops at Length - 1 because once everything else is in place, the last element must be the largest.
The earlier version of this post had a subtle problem: it swapped inside the inner loop every time it found a candidate, and the inner loop started at 0 instead of i + 1. That code happens to produce sorted output, but it is not selection sort. It does up to O(n²) swaps instead of O(n), which throws away the algorithm’s only advantage. If you have that version in old code, replace it with the one above.
Complexity
| Case | Time | Swaps | Extra space |
|---|---|---|---|
| Best | O(n²) | 0 | O(1) |
| Average | O(n²) | O(n) | O(1) |
| Worst | O(n²) | n − 1 | O(1) |
Unlike insertion sort and bubble sort, selection sort gets no faster on sorted input: it still scans the whole unsorted part on every pass.
Selection sort is not stable. The long-distance swap can jump an element over an equal one. Sorting [5a, 5b, 2] swaps 5a with 2, giving [2, 5b, 5a], and the two fives have changed order.
When to use selection sort
Rarely. Insertion sort has the same worst case, is stable, and runs in O(n) on sorted input, so it wins as the simple sort for small arrays. Selection sort’s niche is the minimal number of writes: if each write is slow (flash memory with limited write cycles, a large struct copied by value, or a “swap” that moves something physical), doing at most n − 1 of them can matter.
The “find the minimum, then repeat” idea is also the basis of heap sort, which uses a heap to find each minimum in O(log n) and reaches O(n log n) overall.
For production code, use Array.Sort, List<T>.Sort or span.Sort().
3 thoughts on “Selection Sort in C#: A Generic, Span-Based Implementation”