Introduction to Algorithm - Summary of Chapter 7 - Quicksort

时间:2021-05-07 16:20:36

Introduction of The Chapter

Quicksort algorithm has a worst-case running tine of Θ(n2) , and a expected running time of Θ(nlgn) .It runs in place and is not stable.


Description of quicksort

Quick sort applices the divide-and-conquer paradigm. The three steps of it show :

  1. 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.
  2. Conquer : Sort the two subarrays by recursive calls to quick sort
  3. 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)

Introduction to Algorithm - Summary of Chapter 7 - Quicksort

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 Θ(n) on the subarray A[p..r].


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
T(n)=T(n1)+T(0)+Θ(n)=Θ(n2)
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
T(n)=2T(n/2)+Θ(n)=Θ(nlgn)

Introduction to Algorithm - Summary of Chapter 7 - Quicksort

Introduction to Algorithm - Summary of Chapter 7 - Quicksort

Average-case : The recurrence for running time is still O(nlgn) , but with a slightly larger constant hidden by the O -notation.


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 :
T(n)=max0qn1(T(q)+T(nq1))+Θ(n)
using the substitute model, we assume T(n)cn2 , and θ(n)=c0n .
T(n)cmax0qn1(q2+(nq1)2)+c0n
let f(q)=n2(2q+2)n+2q2+q+1
f(q)=4q2n+1
f′′(q)=4
So, in the range from 0 to n-1, when q is n-1,the f(q) can obtain the maximun value.
T(n)c(n1)2+c0n =cn2+(c02c)n+c
T(n)cn2 when cc0/2
T(n)=O(n2)

using the substitute model, we assume T(n)cn22n , and θ(n)=c0n .
T(n)max0qn1(cq22q+c(nq1)22(nq1))+c0n
T(n)cmax0qn1(q2+(nq1)2(2n+4q+1)/c)+c0n
T(n)cn2c(2n1)+c0n
T(n)cn22n
T(n)=Θ(n2)

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 O(1) , the running time depends on the total number of comparison between two elements. The xi can compare with xj only when both of them are in the same subarray and the xi or xj is the pivot of the subarray. we can assume without generality ,A[i..j] is the continuouse subarray which contain the xi and xj and the pivot is the first element , xi , or the last element , xj .

Prc = Pr{ xi is compared to xj } = Pr{ xi is the pivor of the subarray} + Pr{ xj is the pivor of the subarray}= 1ji+1+1ji+1 = 2ji+1
E[X]=n1i=1nj=i+12ji+1
let k=j-i
E[X]=n1i=1nk=12k+1
E[X]<n1i=1nk=12k=n1i=1O(lgn)
E[X]=O(nlgn)
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

 E[X]=i=1n1j=i+1n2ji+1=i=1n1k=1ni2k+1i=1n1k=1ni22ki=1n1Ω(lgn)=Ω(nlgn)(k1)

Some of above content refere to “Introduction to Algorithm”.