Skip to content

Selection Sort

Category: Classical
Difficulty: Beginner
Time Complexity: O(n²)
Space Complexity: O(1)

Selection Sort divides the array into a sorted region at the front and an unsorted region behind it. On each pass it scans the entire unsorted region to find its minimum, then swaps that minimum into the first unsorted slot. It always performs exactly n(n-1)/2 comparisons regardless of the input, and uses at most n-1 swaps — the fewest writes of any simple sort. It is simple and in-place, but not stable and not adaptive, so it is mainly of educational value.

{
"array": [
64,
25,
12,
22,
11
]
}
{
"array": [
64,
25,
12,
22,
11
]
}
{
"array": [
1,
2,
3,
4,
5
]
}
{
"array": [
5,
4,
3,
2,
1
]
}
{
"array": [
4,
2,
4,
1,
2
]
}
{
"array": [
7
]
}
function selectionSort(array):
for i = 0 to length(array) - 2:
minIndex = i
for j = i + 1 to length(array) - 1:
if array[j] < array[minIndex]:
minIndex = j
swap(array[i], array[minIndex])
return array
def selection_sort(array: list[int]) -> list[int]:
n = len(array)
for i in range(n - 1):
min_index = i
for j in range(i + 1, n):
if array[j] < array[min_index]:
min_index = j
array[i], array[min_index] = array[min_index], array[i]
return array
function selectionSort(array) {
const n = array.length;
for (let i = 0; i < n - 1; i++) {
let minIndex = i;
for (let j = i + 1; j < n; j++) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
[array[i], array[minIndex]] = [array[minIndex], array[i]];
}
return array;
}

Each pass finds the smallest element of the unsorted region and places it at the front. After pass i, the first i+1 elements are in their final sorted positions.

Selection Sort always makes the same number of comparisons — n(n-1)/2 — but only up to n-1 swaps. When writing to memory is expensive, its low swap count is an advantage over Bubble Sort.

Swapping a far-away minimum into place can reorder equal elements, so Selection Sort is not stable. It also does the same work on sorted and unsorted input, so it is not adaptive.

  • Assuming it is faster on sorted input: Unlike Insertion Sort, Selection Sort scans the whole unsorted region every pass, so an already-sorted array still costs O(n²) comparisons.
  • Expecting stability: Selection Sort is not stable. If stability matters (e.g. sorting records by one field), use Insertion Sort or Merge Sort instead.

Q1: How many swaps does Selection Sort perform in the worst case?

  • A) O(1)
  • B) O(n)
  • C) O(n log n)
  • D) O(n²)
Show answer

Answer: B) O(n)

Selection Sort performs at most one swap per pass and there are n-1 passes, so at most n-1 = O(n) swaps.

Q2: Is Selection Sort stable?

  • A) Yes
  • B) No
  • C) Only for sorted input
  • D) Only for distinct values
Show answer

Answer: B) No

Swapping a distant minimum into the front can move an equal element past its equals, breaking stability.

Q3: How many comparisons does Selection Sort make on an already-sorted array of n elements?

  • A) 0
  • B) n - 1
  • C) n(n-1)/2
  • D) n log n
Show answer

Answer: C) n(n-1)/2

It always scans the entire unsorted region, making n(n-1)/2 comparisons regardless of the initial order.