Selection Sort
Scans the unsorted region for its minimum and swaps it to the front. Always exactly n−1 swaps, which matters when writes are expensive — but comparisons stay O(n²).
- Category
- Sorting
- Time complexity
- O(n²)
- Space complexity
- O(1)
Pseudocode
for i ← 0 to n−2 min ← i scan j ← i+1..n−1 for smaller swap A[i], A[min] done
Reference implementation
for i in range(n - 1):
m = i
for j in range(i + 1, n):
if a[j] < a[m]: m = j
a[i], a[m] = a[m], a[i]