Analyzing Selection Sort
Objective: Count how many times cards are handled when you
sort n cards. (Call this H(n))
- First pass: All n cards are handled one or two times
- Second pass: n-1 cards are handled one or two times
- Third pass: n-2 cards are handled one or two times
...
n + (n-1) + (n-2) + ... + 1
is at most n2 and at least n2/4
(n/2 of the numbers are n/2 or larger). So:
n2/4 ≤ H(n) ≤ 2n2
(Factor of 2 on the upper bound because each card is handled one or
two times.)