Sorting Algorithm
Selection Sort
Repeatedly scans the unsorted region for its smallest value and swaps it into place — always exactly one swap per pass, which makes it attractive whenever writes are far more expensive than comparisons.
BestO(n²)
AverageO(n²)
WorstO(n²)
SpaceO(1)
StableNo
Try It — Animated Flow
29
8
44
15
3
37
21
Selection sort repeatedly scans for the smallest remaining value and places it next.
Step 1 / 46
Pointer / current
Comparing
Swapping / removing
Newly inserted
Settled
Java Implementation
Real-Life Usage
Flash memory / EEPROM writes
Selection sort performs at most n swaps total, minimizing wear on storage where every write has a real cost, unlike bubble/insertion sort's many small swaps.
Picking top-K winners
Repeatedly 'selecting the minimum (or maximum)' is exactly how you'd pull out the top 3 finishers from a race without sorting the whole field.
Teaching the 'find the extreme' pattern
Its scan-for-the-best-candidate loop is the same pattern used in many search and optimization algorithms, making it a useful teaching stepping stone.
Small, fixed-size scoreboards
For a small, bounded list (e.g. sorting 5 difficulty tiers), its predictable O(n²) behavior with minimal swaps is simple and good enough.
Trade-offs
Reach for it when
- Writes/swaps are expensive relative to comparisons
- The dataset is small
- You want a simple, predictable number of swaps (at most n)
Avoid it when
- The dataset is large — it's always O(n²), even on sorted input
- You need a stable sort (equal elements can be reordered)
- You need a general-purpose production sort (use Arrays.sort / Collections.sort)