1 / 5

CS 330: Algorithms Quick Sort

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:

ull
Download Presentation

CS 330: Algorithms Quick Sort

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. CS 330: AlgorithmsQuick Sort Gene Itkis

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

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

  4. 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  2all i=1..n ln n  2 n ln n  1.4 n lg n Gene Itkis

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

More Related