How Selection Sort Works
Selection sort divides the input into a sorted and unsorted region. It repeatedly selects the smallest element from the unsorted region and moves it to the end of the sorted region. While simple, it always performs O(n²) comparisons regardless of input order.
Scan the unsorted portion for the minimum element, then swap it with the first unsorted position. Advance the boundary between sorted and unsorted by one, and repeat until the entire array is sorted.
Complexity
- Time
- O(n²)
- Space
- O(1)
- Stability
- unstable
When to Use Selection Sort
- Educational purposes
- Situations where the number of swaps must be minimized
- Small datasets where simplicity is preferred