50 likes | 135 Views
CS 330: Algorithms Quick Sort. Gene Itkis. QuickSort: randomly divide & concur. QSort(A[], F, L): if F>L then return; k Partition (A[], F, L); QSort (A[], F, k-1); QSort (A[], k+1, L); ----------------------- Worst case: T(n)=O(n)+ T(n-1) T(n)=O(n 2 ) Best Case:
E N D
CS 330: AlgorithmsQuick Sort Gene Itkis
QuickSort: randomly divide & concur QSort(A[], F, L): • if F>L then return; • k Partition(A[], F, L); • QSort(A[], F, k-1); • QSort(A[], k+1, L); ----------------------- • Worst case: • T(n)=O(n)+ T(n-1) T(n)=O(n2) • Best Case: • T(n)=O(n)+ 2T(n/2) T(n)=O(n lg n) • Average: ??? O(L-F) Gene Itkis
QSort Analysis • Average Performance = expected #compares • Notation: • zi = i-th smallest element of A[1..n] • Ci,j=Cj,i = the cost of comparing zi and zj • Pi,j = the probability of QSort comparing zi and zj • E[#compares]= all i,j>i Ci,j Pi,j =all i,j>i Pi,j • zi and zj are not compared if for some k: i<k<j, zk is chosen as a pivot before either zi or zj • (only pivot is compared with other elements) • Pi,j=2/(j-i+1) • ( 2 elements –zi, zj– out of j-i+1 result in the comparison ) Gene Itkis
QSort Analysis • Thus • E[#compares]=all i,j>i Pi,j = all i,j>i 2/(j-i+1)< all i,j>i 2/(j-i) • Let j=i+d, where 0<d n-i. Then • E[#compares]< all i,j>i 2/(j-i)= all i,d 2/d = = all i=1..n d=1..n-i2/d < all i=1..n d=1..n 2/d 2all i=1..n ln n 2 n ln n 1.4 n lg n Gene Itkis
Randomized Algorithms vs. Average performance • Average over • Inputs distribution • Always same performance for the same input • E.g. deterministic QSort (say pivot 1st element): ordered arrays always O(n2) • Internal coin-flips distribution • On any input most of the times get better performance although sometimes can slow down to the worst case • Example: QSort (w/randomized partition) most of the times runs in O(n lg n), but occasionally can be as bad as O(n2) • Not good for real-time and/or critical systems Gene Itkis