Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
For each position i, scan i..n-1 for the minimum index, then swap it into position i. O(n²) comparisons, at most n swaps.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you implement min-selection with a single swap per position.
Where candidates slip
Eager swapping (extra writes) or a wrong inner-scan start.
How to stand out
Highlight the O(n) swap count as its distinguishing trait.
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Select-min and swap
1Hint 1Explore hintHide hint
Each step places which element into its final spot?
2Hint 2Explore hintHide hint
Find the min index of the suffix, then swap once.
3Hint 3Explore hintHide hint
Compare its swap count to bubble sort's.
A · Core idea
Selection sort grows a sorted prefix by repeatedly finding the minimum of the unsorted suffix and swapping it to the front of that suffix.
Why track the min index, not swap eagerly. Scan the suffix recording the index of the smallest element, then do a single swap into position i. Swapping every time you see a smaller value would do many extra writes.
Its signature property: few swaps. It always does O(n²) comparisons, but only O(n) swaps (one per position). When writes are expensive (e.g. to flash memory), that minimal-write property is its advantage over bubble sort.
Not stable, in place. It can reorder equal elements, and uses O(1) extra space. Like the other O(n²) sorts, it's a fundamentals algorithm.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
nums = [3, 1, 2]i=0
min is 1 at idx1
[1,3,2][1, 2, 3]C · Code with teaching comments
D · Why it works and complexityO(n²) timeO(1) spaceOpen the invariant and proofExplore the proofHide the proof
ComplexityO(n²) time, O(1) space — n positions each scanning the remaining suffix → O(n²) comparisons, but only O(n) swaps.
E · Edge cases and pitfalls5nums = [5, 4] — One selection → [4, 5].Explore edge casesHide edge cases
Edge cases handled
nums = [5, 4]One selection → [4, 5].nums = [1]Trivial.nums = [1, 2, 3]Still O(n²) comparisons even when sorted.
Pitfalls
- Swapping values instead of tracking the min index.
- Starting the inner scan at i instead of i+1 (harmless but wasteful).
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic void selectionSort(int[] nums) { for (int i = 0; i < nums.length - 1; i++) { int min = i; for (int j = i + 1; j < nums.length; j++) { if (nums[j] < nums[min]) min = j;Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
- Swapping values instead of tracking the min index.
- Starting the inner scan at i instead of i+1 (harmless but wasteful).
Revise
Carry the pattern into the interview
Pattern to remember
Select-min, swap
Code pattern to memorize
for i: m=argmin(a[i..]); swap a[i], a[m]Rules to remember
- 1Find min of suffix.
- 2Swap to position i.
- 3≤ n swaps total.
Use this pattern when
- When swaps/writes are costly
Interview takeaway
Selection sort minimises swaps at the cost of always O(n²) compares.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Select-min, swap. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups