Selection Sort
Software Engineer path · lesson 10 of 99A simple comparison-based sorting algorithm. It divides the list into sorted and unsorted regions, repeatedly selecting the minimum element from the unsorted region.
Up to 12 whole numbers between -99 and 99, separated by commas or spaces.
Start: each pass selects the smallest remaining value and puts it at the front.
- 1for i in 0 .. n-2:
- 2 min = i
- 3 for j in i+1 .. n-1:
- 4 if a[j] < a[min]: min = j
- 5 swap(a[i], a[min])
- 6return a
- Comparisons
- 0 / 21
- Swaps
- 0 / 5
Tap any element in the diagram to inspect its state at this step.
Keys: ← → step, Space play/pause. Predict mode pauses before key steps and asks what happens next.
One pass of selection sort
Order what happens in a single pass.
Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.
- 1Drop a step here
- 2Drop a step here
- 3Drop a step here
- 4Drop a step here
- 5Drop a step here
Test your knowledge
How many swaps does Selection Sort perform in the worst case?
The short version
Imagine picking the smallest candy from a jar one at a time. Each time you pick, you scan ALL remaining candies to find the smallest, then swap it to the front.
In more depth
How It Works
Selection Sort maintains two regions: sorted (left) and unsorted (right).
Algorithm:
Key Properties
When to reach for this
- When swap cost is significantly higher than comparison cost
- When memory writes are expensive (flash memory)
- When simplicity is prioritized over efficiency
Interview Questions
References & Further Reading
Love this visualization?
Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.
100% free • No credit card required