190 likes | 356 Views
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.
E N D
CSE 780: Design and Analysis of Algorithms Lecture 6: Quick sort Deterministic Randomized CSE 2331/5331
Sorting Revisited ! • Quick-sort • Divide and conquer paradigm • But in place sorting • Worst case: • Randomized quicksort: • Expected running time: CSE 2331/5331
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
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
Plan: take A[r]as pivot return m x Partition ( A, p, r) In-place partition ! y CSE 2331/5331
z y x x Partition ( A, p, r ) y Case 1: y > x CSE 2331/5331
y z x x Partition ( A, p, r ) z y Case 2: otherwise CSE 2331/5331
Complexity: O (p - r) CSE 2331/5331
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
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
Remarks • If input array is sorted or reversely sorted: • Quadratic time complexity • Mixing splits • Good ones will dominate CSE 2331/5331
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
Question • How can we have a good mix ? • Intuitively, we will use randomization to achieve that. CSE 2331/5331
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
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
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
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
Time Complexity? • Intuitively, with constant probability, the pivot will cause a balanced partition. • How to make this more rigorous? CSE 2331/5331