Introduction to Algorithm - Summary of Chapter 7 - Quicksort

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)

The show of Partition execution

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)

The show of 9-to-1 permutation after each partition

The of two special situation :the best and the worst node of recurrence

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+(nq

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值