1 / 18

CSE 780: Design and Analysis of Algorithms

CSE 780: Design and Analysis of Algorithms. Lecture 6: Quick sort Deterministic Randomized. Sorting Revisited !. Quick-sort Divide and conquer paradigm But in place sorting Worst case: Randomized quicksort: Expected running time: . A[m]: pivot. Divide and Conquer.

nevin
Download Presentation

CSE 780: Design and Analysis of 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. CSE 780: Design and Analysis of Algorithms Lecture 6: Quick sort Deterministic Randomized CSE 2331/5331

  2. Sorting Revisited ! • Quick-sort • Divide and conquer paradigm • But in place sorting • Worst case: • Randomized quicksort: • Expected running time: CSE 2331/5331

  3. A[m]: pivot Divide and Conquer QuickSort( A, r, s ) m = Partition ( A, r, s ); A1 = QuickSort ( A, r, m-1 ); A2 = QuickSort ( A, m+1, s ); Merge (A1, A2); MergeSort ( A, 1, n ) m = n / 2; A1 = MergeSort ( A, 1, m ); A2 = MergeSort ( A, m+1, n ); Merge (A1, A2); If ( r ≥ s) return; CSE 2331/5331

  4. Partition • m = Partition(A, p, r) • Afterwards • All elements in A[p, …, m-1] have value at most A[m] • All elements in A[m, …, r] have value at least A[m] CSE 2331/5331

  5. Plan: take A[r]as pivot return m x Partition ( A, p, r) In-place partition ! y CSE 2331/5331

  6. z y x x Partition ( A, p, r ) y Case 1: y > x CSE 2331/5331

  7. y z x x Partition ( A, p, r ) z y Case 2: otherwise CSE 2331/5331

  8. Complexity: O (p - r) CSE 2331/5331

  9. Quicksort ( A, r, s) QuickSort( A, p, r ) if ( p ≥ r) return; m = Partition ( A, p, r ); A1 = QuickSort ( A, p, m-1 ); A2 = QuickSort ( A, m+1, r ); In-place Initial call is QuickSort(A, 1, n). CSE 2331/5331

  10. Complexity • T(n) = T(m-1) + T(n-m) + n • Worst case: • T(n) = T(0) + T(n-1) + n = T(n-1) + n • Best case: • T(n) = 2T(n/2) + n CSE 2331/5331

  11. Remarks • If input array is sorted or reversely sorted: • Quadratic time complexity • Mixing splits • Good ones will dominate CSE 2331/5331

  12. Balanced Partitioning • Imagine instead of splitting in the middle, we has a 9-to-1 split • O(n lg n) • Any constant fraction α works the same: • When we have a mixture of good and bad partitioning, the good ones dominates. CSE 2331/5331

  13. Question • How can we have a good mix ? • Intuitively, we will use randomization to achieve that. CSE 2331/5331

  14. Expected Complexity • Probabilistic method: • Given a distribution for all possible inputs • Derive expected time based on distribution • Randomized algorithm: • Add randomness in the algorithm • Analyze the expected behavior of the algorithm CSE 2331/5331

  15. Best: • T(n) =  ( ) n n n 2 2 2 • For all possible inputs, average time • A(n) = 1/2 n lg n + 1/2 =  ( ) Probabilistic Analysis • Simple example: • Assume there are only two types of inputs: • half are best cases, half are worst Randomization will not assume input distribution • Worst: • T(n) =  ( n lg n ) CSE 2331/5331

  16. Randomized-QuickSort( A, p, r ) • If ( p ≥ r)return; • m = Randomized-Partition ( A, p, r ); • A1 = Randomized-QuickSort( A, p, m-1 ); • A2 = Randomized-QuickSort( A, m+1, r ); CSE 2331/5331

  17. y x y Randomly choose position s, takey = A[s] as pivot. Random-Partition ( A, r, s ) x Next, run the same Partition (A, p, r) CSE 780 Algorithms

  18. Time Complexity? • Intuitively, with constant probability, the pivot will cause a balanced partition. • How to make this more rigorous? CSE 2331/5331

More Related