← Back to Sorts

Selection Sort

Selection sort is the brute-force honest one. Look at the whole unsorted pile, find the smallest thing in it, and swap that to the front. Now the front is locked, so do it again with what’s left. Every pass nails down exactly one more value, left to right, until there’s nothing left to pick.

What’s interesting is what it doesn’t do. It never quits a scan early, so it always makes the same n² comparisons whether you feed it sorted, reversed, or shuffled input. Try the Sorted input and watch the comparison count come out identical to Reversed.

Hit Play to watch it run, or use Next and Back to step one comparison at a time. Shuffle deals a fresh array, and the highlighted line shows you where the code is at each step.