selection-sort.js
function selectionSort(a) {
for (let i = 0; i < a.length; i++) {
let min = i;
for (let j = i + 1; j < a.length; j++) {
if (a[j] < a[min]) min = j;
}
const tmp = a[i];
a[i] = a[min];
a[min] = tmp;
}
return a;
}
selectionSort([4, 1, 7, 3, 2]);
What to watch
The min pointer scans ahead; one swap per pass moves the smallest value to the front.
Reading selection sorton a page only gets you so far - code is a process, and the process happens at runtime where you can't normally see it. In NeonFlow, this exact program runs step by step: every call opens a frame, every branch picks a path, and every value moves through memory in front of you. That's the fastest way to actually understand how selection sort works.
Run selection sort yourself
Open NeonFlow, paste this code (or any of your own), and scrub through it one step at a time.
Open NeonFlow