Introduction of The Chapter
Quicksort algorithm has a worst-case running tine of
Description of quicksort
Quick sort applices the divide-and-conquer paradigm. The three steps of it show :
- Divide : Partition the array A[p..r] into two subarrays A[p..q-1] and A[q+1..r] such that each element of A[p..q-1] is less than or equal to A[p], which is , in turn , less than or equal to eack element of A[q+1..r]. Compute the index q as part of this partition procedure.
- Conquer : Sort the two subarrays by recursive calls to quick sort
- Combine : Because the two subarrays are already sorted, no work is needed to combine them.
Quick-Sort (A, p, r)
if p < r
q = Partition (A, p, r)
Quick-Sort (A, p, q-1)
Quick-Sort (A, q+1,r)
Partition (A, p, r)
x = A[r]
i = p - 1
for j=p to r-1
if A[j] <= x
i = i+1
exchange A[i] with A[j]
exchange A[i+1] with A[r]
return i+1
The Partition has a running time of
Performance of quick sort
Worst-case : The every call of Partition partition the array into a subarray with 0 element and n-1 elements. So the recurrence for running time is
The worst-case running time occurs when the input is already completely sorted.
Best-case : Partition produce two subproblems, each of size no more than n/2. So the recurrence for running time is
Average-case : The recurrence for running time is still
A randomized version of quicksort
Selecting randomly the chosen element from A[p..r] to be the pivot.
Randomized-Partition (A, p, r)
i = Random (p, r)
exchange A[r] with a[i]
return Partition (A, p, r)
Randomized-QuickSort (A, p, r)
if p < r
q = Randomized-Paritition (A, p, r)
Randomized-QuickSort (A, p, q-1)
Randomized-QuickSort (A, q+1, r)
Analysis of quicksort
Worst-case :
using the substitute model, we assume
let
So, in the range from 0 to n-1, when q is n-1,the
using the substitute model, we assume
expected running time :
After each time thePartition procedure is called, the pivot which product never appears in any calls of Quicksort or Partition. So there are at most n calls to Partition.
Because the call to Partition takes
Prc = Pr{
let k=j-i
Introduction to Algorithms
Exercise index
About this site
Exercise 7.4.4
Show that RANDOMIZED-QUICKSORT’s expected running time is \Omega(n\lg{n}).
We use the same reasoning for the expected number of comparisons, we just take in in a different direction.align
Some of above content refere to “Introduction to Algorithm”.