1 / 14

Introduction to Data Structures and Algorithms

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.

Download Presentation

Introduction to Data Structures and Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Data Structures and Algorithms Chapter 7 Quick Sort

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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)

  9. 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.

  10. 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.

  11. 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

  12. 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

  13. 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).

  14. 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)

More Related