[prev] 20 [next]

Selection Sort (cont)

Cost analysis (where n = hi-lo+1):
  • on first pass, n-1 comparisons, 1 swap
  • on second pass, n-2 comparisons, 1 swap
  • ... on last pass, 1 comparison, 1 swap
  • C = (n-1)+(n-2)+...+1 = n*(n-1)/2 = (n2-n)/2O(n2)
  • S = n-1
Cost is same, regardless of sortedness of original array.