These are part of my notes for the Coursera course: Algorithms, Pt I (Princeton).
##Selection Sort
####Concept
- iterate through all items to find the smallest
- swap the found item with the front-most item | Repeat
- iterate through all remaining items to find the smallest |
####Analysis
iterate N times one less item each time
| |
(c1 * N) * c2 + (c1 * N-1) * c2 + (c1 * N-2) * c2 + ... + c1 * c2
|
perform swap
= c2 * (N - 1) + c1 * (1 + 2 + 3 + ... + N)
= c2 * (N - 1) + c1 * N*(N + 1)/2
~ 1/2 * N^2
TLDR Selection sort takes time:
~N-1 compares and ~1/2 * N^2 exchanges
Selection sort is independent of input order - it will always take the same amount of time for a given input size.