Download Presentation
## Sorting

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**Sorting**Practice with Analysis**Repeated Minimum**• Search the list for the minimum element. • Place the minimum element in the first position. • Repeat for other n-1 keys. • Use current position to hold current minimum to avoid large-scale movement of keys.**Repeated Minimum: Code**Fixed n-1 iterations for i := 1 to n-1 do for j := i+1 to n do if L[i] > L[j] then Temp = L[i]; L[i] := L[j]; L[j] := Temp; endif endfor endfor Fixed n-i iterations**Repeated Minimum: Analysis**Doing it the dumb way: The smart way: I do one comparison when i=n-1, two when i=n-2, … , n-1 when i=1.**Bubble Sort**• Search for adjacent pairs that are out of order. • Switch the out-of-order keys. • Repeat this n-1 times. • After the first iteration, the last key is guaranteed to be the largest. • If no switches are done in an iteration, we can stop.**Bubble Sort: Code**Worst case n-1 iterations for i := 1 to n-1 do Switch := False; for j := 1 to n-i do if L[j] > L[j+1] then Temp = L[j]; L[j] := L[j+1]; L[j+1] := Temp; Switch := True; endif endfor if Not Switch then break; endfor Fixed n-i iterations**Bubble Sort Analysis**Being smart right from the beginning:**Insertion Sort I**• The list is assumed to be broken into a sorted portion and an unsorted portion • Keys will be inserted from the unsorted portion into the sorted portion. Unsorted Sorted**Insertion Sort II**• For each new key, search backward through sorted keys • Move keys until proper position is found • Place key in proper position Moved**Insertion Sort: Code**Fixed n-1 iterations for i:= 2 to n do x := L[i]; j := i-1; while j<0 and x < L[j] do L[j-1] := L[j]; j := j-1; endwhile L[j+1] := x; endfor Worst case i-1 comparisons**Insertion Sort: Analysis**• Worst Case: Keys are in reverse order • Do i-1 comparisons for each new key, where i runs from 2 to n. • Total Comparisons: 1+2+3+ … + n-1**Insertion Sort: Average I**• Assume: When a key is moved by the While loop, all positions are equally likely. • There are i positions (i is loop variable of for loop) (Probability of each: 1/i.) • One comparison is needed to leave the key in its present position. • Two comparisons are needed to move key over one position.**Insertion Sort Average II**• In general: k comparisons are required to move the key over k-1 positions. • Exception: Both first and second positions require i-1 comparisons. Position 1 2 3 ... i-2 i-1 i ... ... i-1 i-1 i-2 3 2 1 Comparisons necessary to place key in this position.**Insertion Sort Average III**Average Comparisons to place one key Solving**Insertion Sort Average IV**For All Keys:**Optimality Analysis I**• To discover an optimal algorithm we need to find an upper and lower asymptotic bound for a problem. • An algorithm gives us an upper bound. The worst case for sorting cannot exceed (n2) because we have Insertion Sort that runs that fast. • Lower bounds require mathematical arguments.**Optimality Analysis II**• Making mathematical arguments usually involves assumptions about how the problem will be solved. • Invalidating the assumptions invalidates the lower bound. • Sorting an array of numbers requires at least (n) time, because it would take that much time to rearrange a list that was rotated one element out of position.**Rotating One Element**Assumptions: Keys must be moved one at a time All key movements take the same amount of time The amount of time needed to move one key is not dependent on n. 2nd 1st n keys must be moved 3rd 2nd 4th 3rd (n) time nth n-1st 1st nth**Other Assumptions**• The only operation used for sorting the list is swapping two keys. • Only adjacent keys can be swapped. • This is true for Insertion Sort and Bubble Sort. • Is it true for Repeated Minimum? What about if we search the remainder of the list in reverse order?**Inversions**• Suppose we are given a list of elements L, of size n. • Let i, and j be chosen so 1i<jn. • If L[i]>L[j] then the pair (i,j) is an inversion. Not an Inversion 1 2 3 5 4 10 6 7 8 9 Inversion Inversion Inversion**Maximum Inversions**• The total number of pairs is: • This is the maximum number of inversions in any list. • Exchanging adjacent pairs of keys removes at most one inversion.**Swapping Adjacent Pairs**The only inversion that could be removed is the (possible) one between the red and green keys. Swap Red and Green The relative position of the Red and blue areas has not changed. No inversions between the red key and the blue area have been removed. The same is true for the red key and the orange area. The same analysis can be done for the green key.**Lower Bound Argument**• A sorted list has no inversions. • A reverse-order list has the maximum number of inversions, (n2) inversions. • A sorting algorithm must exchange (n2) adjacent pairs to sort a list. • A sort algorithm that operates by exchanging adjacent pairs of keys must have a time bound of at least (n2).**Lower Bound For Average I**• There are n! ways to rearrange a list of n elements. • Recall that a rearrangement is called a permutation. • If we reverse a rearranged list, every pair that used to be an inversion will no longer be an inversion. • By the same token, all non-inversions become inversions.**Lower Bound For Average II**• There are n(n-1)/2 inversions in a permutation and its reverse. • Assuming that all n! permutations are equally likely, there are n(n-1)/4 inversions in a permutation, on the average. • The average performance of a “swap-adjacent-pairs” sorting algorithm will be (n2).**Quick Sort I**• Split List into “Big” and “Little” keys • Put the Little keys first, Big keys second • Recursively sort the Big and Little keys Little Big Pivot Point**Quicksort II**• Big is defined as “bigger than the pivot point” • Little is defined as “smaller than the pivot point” • The pivot point is chosen “at random” • Since the list is assumed to be in random order, the first element of the list is chosen as the pivot point**Quicksort Split: Code**Points to last element in “Small” section. Split(First,Last) SplitPoint := 1; for i := 2 to n do if L[i] < L[1] then SplitPoint := SplitPoint + 1; Exchange(L[SplitPoint],L[i]); endif endfor Exchange(L[SplitPoint],L[1]); return SplitPoint; End Split Fixed n-1 iterations Make “Small” section bigger and move key into it. Else the “Big” section gets bigger.**Quicksort III**• Pivot point may not be the exact median • Finding the precise median is hard • If we “get lucky”, the following recurrence applies (n/2 is approximate)**Quicksort IV**• If the keys are in order, “Big” portion will have n-1 keys, “Small” portion will be empty. • N-1 comparisons are done for first key • N-2 comparisons for second key, etc. • Result:**QS Avg. Case: Assumptions**• Average will be taken over Location of Pivot • All Pivot Positions are equally likely • Pivot positions in each call are independent of one another**QS Avg: Formulation**• A(0) = 0 • If the pivot appears at position i, 1in then A(i-1) comparisons are done on the left hand list and A(n-i) are done on the right hand list. • n-1 comparisons are needed to split the list**QS Avg: Solving Recurr.**Guess: a>0, b>0**QS Avg: Continuing**By Integration:**Merge Sort**• If List has only one Element, do nothing • Otherwise, Split List in Half • Recursively Sort Both Lists • Merge Sorted Lists**The Merge Algorithm**Assume we are merging lists A and B into list C. Ax := 1; Bx := 1; Cx := 1; while Ax n and Bx n do if A[Ax] < B[Bx] then C[Cx] := A[Ax]; Ax := Ax + 1; else C[Cx] := B[Bx]; Bx := Bx + 1; endif Cx := Cx + 1; endwhile while Ax n do C[Cx] := A[Ax]; Ax := Ax + 1; Cx := Cx + 1; endwhile while Bx n do C[Cx] := B[Bx]; Bx := Bx + 1; Cx := Cx + 1; endwhile**Merge Sort: Analysis**• Sorting requires no comparisons • Merging requires n-1 comparisons in the worst case, where n is the total size of both lists (n key movements are required in all cases) • Recurrence relation:**Merge Sort: Space**• Merging cannot be done in place • In the simplest case, a separate list of size n is required for merging • It is possible to reduce the size of the extra space, but it will still be (n)**Heapsort: Heaps**• Geometrically, a heap is an “almost complete” binary tree. • Vertices must be added one level at a time from right to left. • Leaves must be on the lowest or second lowest level. • All vertices, except one must have either zero or two children.**Heapsort: Heaps II**• If there is a vertex with only one child, it must be a left child, and the child must be the rightmost vertex on the lowest level. • For a given number of vertices, there is only one legal structure**Heapsort: Heap Values**• Each vertex in a heap contains a value • If a vertex has children, the value in the vertex must be larger than the value in either child. • Example: 20 7 19 5 6 12 2 10 3**Heapsort: Heap Properties**• The largest value is in the root • Any subtree of a heap is itself a heap • A heap can be stored in an array by indexing the vertices thus: • The left child of vertexv has index 2v andthe right child hasindex 2v+1 1 3 2 6 7 4 5 8 9**Heapsort: FixHeap**• The FixHeap routine is applied to a heap that is geometrically correct, and has the correct key relationship everywhere except the root. • FixHeap is applied first at the root and then iteratively to one child.**Heapsort FixHeap Code**FixHeap(StartVertex) v := StartVertex; while 2*v n do LargestChild := 2*v; if 2*v < n then if L[2*v] < L[2*v+1] then LargestChild := 2*v+1; endif endif if L[v] < L[LargestChild] Then Exchange(L[v],L[LargestChild]); v := LargestChild else v := n; endif endwhile end FixHeap n is the size of the heap Worst case run time is (lg n)**Heapsort: Creating a Heap**• An arbitrary list can be turned into a heap by calling FixHeap on each non-leaf in reverse order. • If n is the size of the heap, the non-leaf with the highest index has index n/2. • Creating a heap is obviously O(n lg n). • A more careful analysis would show a true time bound of (n)**Heap Sort: Sorting**• Turn List into a Heap • Swap head of list with last key in heap • Reduce heap size by one • Call FixHeap on the root • Repeat for all keys until list is sorted