← Algorithms · Lesson 3/11
Selection sort
▶ Watch it as a video: one minute with LeIA
Find the minimum and put it in front
Selection sort does what you'd do with tiles scattered on a table: find the smallest and put it first; of the rest, find the smallest and put it second; and so on.
In the program, i is the slot to fill and min remembers where the smallest one seen so far is. The j loop checks everything that's left; when it ends, the minimum is swapped into slot i and that slot is never touched again.
$ node leccion.js
Press RUN (or Ctrl+Enter).
What to look at
- The comparison is
<, not<=: with repeated numbers it keeps the first one it found. - Each pass makes exactly one swap, even if it's a number with itself.
The whole lesson on one page
Find the minimum and put it in front
Selection sort does what you'd do with tiles scattered on a table: find the smallest and put it first; of the rest, find the smallest and put it second; and so on.
In the program, i is the slot to fill and min remembers where the smallest one seen so far is. The j loop checks everything that's left; when it ends, the minimum is swapped into slot i and that slot is never touched again.
function seleccion(a) {
const n = a.length;
for (let i = 0; i < n - 1; i++) {
let min = i;
for (let j = i + 1; j < n; j++) {
if (a[j] < a[min]) min = j;
}
const t = a[i];
a[i] = a[min];
a[min] = t;
}
return a;
}
const lista = [5, 2, 8, 3, 7, 1, 6, 4];
console.log(seleccion(lista));
- The comparison is
<, not<=: with repeated numbers it keeps the first one it found. - Each pass makes exactly one swap, even if it's a number with itself.
Step by step
The same function, printing the array at the end of each pass and counting the comparisons.
function seleccion(a) {
const n = a.length;
let comparaciones = 0;
for (let i = 0; i < n - 1; i++) {
let min = i;
for (let j = i + 1; j < n; j++) {
comparaciones++;
if (a[j] < a[min]) min = j;
}
[a[i], a[min]] = [a[min], a[i]];
console.log("i =", i, "→", ...a);
}
console.log("comparisons:", comparaciones);
console.log("swaps:", n - 1);
}
seleccion([5, 2, 8, 3, 7, 1, 6, 4]);
- After each line, the first
i + 1are already in their final place: unlike insertion sort, they never move again. - 28 comparisons, the same as bubble sort, and only 7 swaps.
Always the same work
Selection sort doesn't notice anything: even if the list is already sorted, it checks everything that's left on every pass. It's always n(n - 1)/2 comparisons: O(n²) in the best and the worst case.
Its strength is something else: it makes at most n - 1 swaps, far fewer than bubble sort. When writing costs much more than reading (say, memory that wears out as you write to it), that matters.
function cuenta(a) {
let comparaciones = 0, intercambios = 0;
for (let i = 0; i < a.length - 1; i++) {
let min = i;
for (let j = i + 1; j < a.length; j++) {
comparaciones++;
if (a[j] < a[min]) min = j;
}
[a[i], a[min]] = [a[min], a[i]];
intercambios++;
}
return comparaciones + " comparisons, " + intercambios + " swaps";
}
const ordenada = Array.from({ length: 1000 }, (_, i) => i + 1);
console.log("sorted: ", cuenta(ordenada));
console.log("reversed:", cuenta([...ordenada].reverse()));
- Sorted or reversed, it does exactly the same: 499,500 comparisons and 999 swaps.
- Insertion sort, on the sorted list, made 999 comparisons and no moves.