Download presentation
Presentation is loading. Please wait.
1
Computer Science CS 330: Algorithms Quick Sort Gene Itkis
2
Computer Science CS-330: Algorithms, Fall 2008Gene Itkis2 QuickSort: randomly divide & concur QSort(A[], F, L): if F>L then return; k Partition(A[], F, L); QSort QSort(A[], F, k-1); QSort QSort(A[], k+1, L); ----------------------- Worst case: T(n)=O(n)+ T(n-1) T(n)=O(n 2 ) Best Case: T(n)=O(n)+ 2T(n/2) T(n)=O(n lg n) Average: ??? O(L- F)
3
Computer Science CS-330: Algorithms, Fall 2008Gene Itkis3 QSort Analysis Average Performance = expected #compares Notation: z i = i-th smallest element of A[1..n] C i,j =C j,i = the cost of comparing z i and z j P i,j = the probability of QSort comparing z i and z j E[#compares]= all i,j>i C i,j P i,j = all i,j>i P i,j z i and z j are not compared if for some k: i<k<j, z k is chosen as a pivot before either z i or z j (only pivot is compared with other elements) P i,j =2/(j-i+1) ( 2 elements – z i, z j – out of j-i+1 result in the comparison )
4
Computer Science CS-330: Algorithms, Fall 2008Gene Itkis4 QSort Analysis Thus +1 E[#compares] = all i,j>i P i,j = all i,j>i 2/(j-i+1) i 2/(j-i) Let j=i+d, where 0<d n-i. Then n E[#compares] i 2/(j-i)= all i,d 2/d = = all i=1..n d=1..n-i 2/d < all i=1..n d=1..n 2/d 2 all i=1..n ln n 2 n ln n 1.4 n lg n
5
Computer Science CS-330: Algorithms, Fall 2008Gene Itkis5 Randomized Algorithms vs. Average performance Average over Inputs distribution Always same performance for the same input O(n 2 ) E.g. deterministic QSort (say pivot 1 st element): ordered arrays always O(n 2 ) Internal coin-flips distribution On any input most of the times get better performance although sometimes can slow down to the worst case O(n lg n) O(n 2 ) Example: QSort (w/randomized partition) most of the times runs in O(n lg n), but occasionally can be as bad as O(n 2 ) Not good for real-time and/or critical systems
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.