Min-based sort (Really, called Selection Sort) ______________ We want to sort a list of numbers in ascending order. The simplest possible sort is one we have seen before. Suppose the list has n elements L is the list. Suppose L is: 11 3 2 22 1 (5 elements) Pass # 1: to get the smallest number into location 0. Set min = L[0] = 11 (don't know if it is min; just pretend it is now) Compare min to L[1]. If (L[1] < min) then swap. 3 11 2 22 1 (rest of the list untouched so far) We know "current" min = 3, since it is in L[0]. Compare min to L[2]. If (L[2] < min) then swap. 2 11 3 22 1 (rest of list untouched so far) "current" min is 2, in L[0]. Compare it to L[3]. If (min < L[3]) swap. 2 11 3 22 1 (no swap this time since 2 < 22) "current" min is still 2. Compare it to L[4]. If (min < L[4]) swap. 1 11 3 22 2 To get the min of the whole list L, it cost us (n-1) comparisons for a size n list. The smallest number is in the right place, location 0. Now we have to sort the rest of the list from L[1] through L[n-1]. _____________________________________________________________________________________ Pass # 2: To get the 2nd minimum (2nd smallest number) in location 1 of list. Simply repeat the above steps starting with min = L[1] = 11. With 3 comparisons, the 2nd minimum (i.e., 2) will end up in location 1. We'll get these sequences: 1 , 11 3 22 2 (start with min = L[1] = 11) 1 , 3 11 22 2 1 , 3 11 22 2 1 , 2 11 22 3 With a sublist of size 4, it took us 3 comparisons. If the original list was of size n, this pass would have taken (n-2) comparisons. We continue in this way. ___________________________________________________________________________________ Pass # 3: 1 2, 11 22 3 (start with min = L[2] = 11) 1 2, 11 22 3 (nothing changes when 11 is compared to 22) 1 2, 3 22 11 For an original list of size n, this pass would have cost (n-3) comparisons ____________________________________________________________________________________ Pass # 4: 1 2 3, 22 11 (start with min = L[3] = 22) 1 2 3, 11 22 For an original list of size this pass would have cost (n-4) comparisons _____________________________________________________________________________ No need for Pass # 5 because L[4] is the last element and because of how we have been swapping numbers, this must be the max number. ___________________________________________________________________________ T(n) = number of comparisons to sort a list of size n = (# comps in pass 1) + (# comps in pass 2) + .... + (# comps in pass (n-1)) = (n-1) + (n-2) + (n-3) + ...... + 1 = ((n-1)*n)/2 = (n^2)/2 - n/2, which is dominated by n^2 So T(n) is in O(n^2) ___________________________________________________________________________ Note: While this sorting algorithm works fine, it seems a bit silly, because in every pass we are visiting EVERY element of the sublist. That means we are touching the same numbers over and over. We should try to get a number into its right place with as few moves as possible. Not easy to do. We may still end up with an O(n^2) algorithm, but there will be some savings in unnecessary moves. ______________________________________________________________________________