- Explain how the sorted and unsorted regions change after each pass.
- Trace the current minimum while scanning an array.
- Implement Selection Sort in JavaScript, Python, Java, or C++.
- Recognize its fixed comparison cost, low swap count, and lack of stability.
Sorting by deliberate selection
Find the smallest.
Place it next.
Selection Sort scans the unsorted region, remembers its smallest value, and moves that value to the next open position.
Key idea After each pass, the sorted region grows by exactly one value from left to right.
Before you start
What you will learn
A clear finish line for the lesson, plus the small amount of knowledge you need before beginning.
- Arrays and zero-based indexes
- Nested loops and simple conditions
- Big O basics from the glossary
01 — The idea
How Selection Sort thinks
It repeats one task: “Which value is smallest in the part I have not sorted yet?”
Mark the boundary
The first unsorted position is where the next smallest value belongs.
Find the minimum
Scan every remaining value and remember the smallest one seen.
Place and repeat
Swap the minimum into the boundary, then move the boundary one step right.
02 — Try it yourself
Watch the minimum move into place
Use your own numbers, then follow the boundary, candidate comparisons, and selected minimum.
Turn each animation into a written decision
| Step | Pass | Action | Candidates / minimum | Array after step | Swaps |
|---|---|---|---|---|---|
| 0 | — | Ready | — | [64, 25, 12, 22, 11, 48, 36] | 0 |
The highlighted row always matches the visualization above. Step forward to extend the trace.
03 — Read the code
From minimum scan to working code
Compare how all four languages remember one index during the scan and swap only after the pass.
01function selectionSort(numbers) {02 const array = [...numbers];03 04 for (let boundary = 0; boundary < array.length - 1; boundary++) {05 let minIndex = boundary;06 07 for (let i = boundary + 1; i < array.length; i++) {08 if (array[i] < array[minIndex]) {09 minIndex = i;10 }11 }12 13 if (minIndex !== boundary) {14 [array[boundary], array[minIndex]] = [array[minIndex], array[boundary]];15 }16 }17 18 return array;19}04 — Avoid the traps
Common mistakes
These bugs often produce an answer that looks almost right. Learn the symptom and the correction together.
Swapping during the scan
Symptom: Values move every time a smaller candidate appears.
Correction: Remember the minimum index and perform at most one swap after the scan.
Resetting the minimum incorrectly
Symptom: A pass compares against an old minimum from the previous pass.
Correction: Initialize the minimum index to the current boundary at the start of every pass.
Skipping the last candidate
Symptom: The smallest value at the right edge is never selected.
Correction: Scan from boundary + 1 through the final array index.
Assuming sorted input is faster
Symptom: The best case is reported as linear time.
Correction: Selection Sort still scans the full unsorted region on every pass, even when no swaps occur.
05 — Check yourself
Check your understanding
Answer 4 questions, then reveal the reasoning. Your progress stays on this device—no account required.
Answer every question to check your work.
06 — Mini game
Choose the minimum
Build the sorted region exactly as Selection Sort does by choosing the smallest package still available.
Choose the smallest value from index 0 onward.
07 — Use it wisely
Where Selection Sort fits
Learning
Its boundary and minimum pointer make loop invariants concrete and visible.
Great fitFew writes
It performs at most n − 1 swaps, useful when writes cost more than comparisons.
Useful propertyTiny datasets
For a handful of values, its simple in-place structure may be sufficient.
Reasonable fitLarge or nearly sorted data
It always performs quadratic comparisons and does not benefit from existing order.
AvoidLesson progress
Finish this lesson with confidence
Use this private checklist to confirm what you learned. Progress stays only in this browser.
Complete every checklist item to finish the lesson.