140 likes | 252 Views
Introduction to Data Structures and Algorithms. Chapter 7 Quick Sort. Quick Sort. The general strategy of QuickSort is to: Partition the list into two sublists, rearranging the keys of a list to be sorted, so that all the “small” keys precede the “large” keys.
E N D
Introduction to Data Structures and Algorithms Chapter 7 Quick Sort
Quick Sort The general strategy of QuickSort is to: • Partition the list into two sublists, rearranging the keys of a list to be sorted, so that all the “small” keys precede the “large” keys. • QuickSort the two sublists recursively
Partition Algorithm select splitVal, which is the pivot value split the list so that value[first] .. value[splitPt-1] < splitVal value[splitPt] = splitVal value[splitPt+1] .. Value[last] > splitVal
Algorithm Implementation(1) function partition return type integer parameters left of type integer right of type integer pivot of type long is variables define leftPtr of type integer define rightPtr of type integer define found of type boolean begin set leftPtr = left set righPtr = right while leftPtr < rightPtr do set found = false // find element larger than pivot while leftPtr < right and found != true do if theArray [leftPtr] < pivot then increment leftPtr else found = true endif endwhile
Algorithm Implementation(2) set found = false // find element smaller than pivot while rightPtr < left and found != true do if theArray [rightPtr] > pivot then decrement rightPtr else found = true endif endwhile // now swap the two elements call swap using leftPtr, rightPtr endwhile return leftPtr // index of partition point endfun partition // end of function
Efficiency of Partition Algorithm • The algorithm has a time complexity of O(N) • The two indices (leftPtr and rightPtr) start at opposite end of the array and move towards each other, swapping as they move • On average, there will be N/2 comparisons
QuickSort function quickSort parameters first of type integer, last of type integer is variables define splitPt of type integer begin if first < last then set splitPt = partition (first, last, pivot) quickSort(first, spliPt-1) quickSort(splitPt+1, last) endif endfun quickSort
Time Complexity of Quick Sort • The time complexity of Quick sort is O(N log N) • It is an in-place algorithm • Ideally, the pivot should be median of the values being sorted • This would be the case in having two subarrays of equal size • In the worst case, the algorithm degenerates to O(N2)
Loop Invariants and Variants • To help verify the correctness of loops • A loop invariant is a boolean expression that evaluates to true every time the loop guard is evaluated. • The boolean expression normally includes variables used in the loop.
Loop Invariants • The initial value of the loop invariant helps determine the proper initial values of variables used in the loop guard and body • In the loop body, some statements make the invariant false, and other statements must then re-establish the invariant so that it is true before the loop guard is evaluated again.
Example function gcd return type integer parameters a of type integer, b of type integer is set x = a set y = b precondition a > 0 and b > 0 begin while x != y do if x > y then set x = x – y else set y = y – x endif endwhile endfun gcd
Loop Invariant Example • In the loop code above, the following boolean expression is true after loop initialization and maintained on every iteration: x > 0 and y > 0 • The OOSimL language syntax allows us to write an additional construct: loopinv X > 0 and y > 0
Loop Variant • The loop variant is an integer expression that always evaluates to a non-negative integer value and decreases on every iteration • The loop variant helps to guarantee that the loop terminates (in a finite number of iterations).
Example • The the gcd function, included above, the variant is: max(x,y). • In OOsimL, the syntax for this construct appears as: loopvariant max (x, y)