Selection Sort
Idea Like You Are 10
Imagine you want to line up kids from shortest to tallest.
You look at everyone, find the shortest kid, and place them first.
Then you look at the remaining kids, find the next shortest, and place them second.
That is Selection Sort.
Small Example
Sort:
Step 1: find the smallest number, which is 1
Step 2: from the remaining part, find the smallest number, which is 3
Step 3: from the remaining part, find the smallest number, which is 5
Diagram
Algorithm
- Find the smallest element in the unsorted part
- Swap it with the first unsorted position
- Move the boundary of the sorted part one step right
- Repeat
Pseudocode
Time Complexity
| Case | Time |
|---|---|
| Best | O(n^2) |
| Average | O(n^2) |
| Worst | O(n^2) |
Why This Time Complexity?
Selection Sort always searches the rest of the array to find the smallest item.
That means:
- first round checks about
nitems - second round checks about
n-1 - then
n-2 - and so on
So total work is:
That becomes about n^2, even if the list was already sorted.
Space Complexity
O(1) because it uses only a few extra variables.
Performance
- Easy to understand
- Usually slower than good
O(n log n)sorts - Makes fewer swaps than Bubble Sort
When To Use
- When swaps are expensive and you want to keep them low
- When teaching the idea of choosing the smallest repeatedly