Selection Sort — Algorithm Visualized
Learn selection sort — find minimum each pass, swap to front, O(n²) comparisons, fewer swaps than bubble sort, with interactive step visualizer.
Introduction
Selection sort divides the array into a sorted prefix and unsorted suffix. Each pass finds the minimum of the suffix and swaps it into the next position — at most n − 1 swaps, but still O(n²) comparisons.
Quick index
| # | Section |
|---|---|
| 1 | How it works |
| 2 | Interactive visualizer |
| 3 | Full implementation |
| 4 | Complexity analysis |
| 5 | Real-life examples |
1. How it works
[ sorted | unsorted ]
↑ ↑
arr[i] scan for min → swapEach pass: scan arr[i..n-1], swap minimum to arr[i]. Sorted region grows by one.
2. Interactive visualizer
Selection Sort — Step Visualizer
Find the minimum each pass and swap it into the growing sorted prefix.
Unsorted
n = 7 elements
Step 1 — Input array
for (let i = 0; i < n - 1; i++) minIdx = argmin(arr[i..n-1]) swap(arr[i], arr[minIdx])
Select minimum from unsorted suffix each pass.
Time
O(n²)
Space
O(1)
Stable
No
3. Full implementation
function selectionSort(arr) {
const n = arr.length
for (let i = 0; i < n - 1; i++) {
let minIdx = i
for (let j = i + 1; j < n; j++) {
if (arr[j] < arr[minIdx]) minIdx = j
}
if (minIdx !== i) [arr[i], arr[minIdx]] = [arr[minIdx], arr[i]]
}
return arr
}Function calls with output
// ── Demo calls — output shown on the right ──
const arr = [5, 3, 8, 1, 9, 2, 7]
selectionSort(arr) // → [1, 2, 3, 5, 7, 8, 9]
selectionSort([3, 1, 2]) // → [1, 2, 3]4. Complexity analysis
| Metric | Value |
|---|---|
| Time | O(n²) always — Θ(n²) comparisons |
| Space | O(1) |
| Swaps | O(n) — at most one per pass |
| Stable | No — long-distance swaps can reorder equals |
5. Real-life examples
| Scenario | Why selection sort |
|---|---|
| Flash/EEPROM memory | Minimizes writes — flash cells wear out; at most n swaps |
| Sorting by cost on a budget | Fewer expensive swap operations when swap cost >> compare cost |
| Small offline devices | Simple logic, no extra buffer — O(1) space |
| Teaching "find minimum" pattern | Core loop mirrors Math.min(...arr) by hand |
| Kiosk with fixed item count | 12 menu items sorted by price once at startup |
Kiosk menu prices
15 items sorted once on boot — selection sort minimizes flash writes.
Index → value
5 kiosk items — unsorted prices in flash memory.
Real-world implementation
function selectionSort(arr) {
const a = [...arr]
for (let i = 0; i < a.length; i++) {
let minIdx = i
for (let j = i + 1; j < a.length; j++) {
if (a[j] < a[minIdx]) minIdx = j
}
if (minIdx !== i) [a[i], a[minIdx]] = [a[minIdx], a[i]]
}
return a
}Full working code for the scenario above.
Function calls with output
// ── Function calls with output ── const menuPrices = [8.00, 4.99, 1.25, 6.75, 2.50]
Side output shows return value after each call — step through to trace execution.
Summary
Selection sort minimizes writes — useful when swap cost dominates. Still O(n²) for comparisons.
Next reads: Insertion Sort · Merge Sort
Subscribe to my newsletter
Stay up to date and get notified when I share new contents.
No spam ever, unsubscribe anytime