Introduction to Algorithm - Summary of Chapter 7 - Quicksort
2015-11-06 21:52
423 查看
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 :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 Θ(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 isT(n)=T(n−1)+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)
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)=max0≤q≤n−1(T(q)+T(n−q−1))+Θ(n)
using the substitute model, we assume T(n)≤cn2, and θ(n)=c0n.
T(n)≤cmax0≤q≤n−1(q2+(n−q−1)2)+c0n
let f(q)=n2−(2q+2)n+2q2+q+1
f′(q)=4q−2n+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(n−1)2+c0n =cn2+(c0−2c)n+c
T(n)≤cn2 when c≥c0/2
T(n)=O(n2)
using the substitute model, we assume T(n)≥cn2−2n, and θ(n)=c0n.
T(n)≥max0≤q≤n−1(cq2−2q+c(n−q−1)2−2(n−q−1))+c0n
T(n)≥cmax0≤q≤n−1(q2+(n−q−1)2−(2n+4q+1)/c)+c0n
T(n)≥cn2−c(2n−1)+c0n
T(n)≥cn2−2n
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{ xiis compared to xj } = Pr{xi is the pivor of the subarray} + Pr{xj is the pivor of the subarray}=1j−i+1+1j−i+1=2j−i+1
E[X]=∑n−1i=1∑nj=i+12j−i+1
let k=j-i
E[X]=∑n−1i=1∑nk=12k+1
E[X]<∑n−1i=1∑nk=12k=∑n−1i=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=1n−1∑j=i+1n2j−i+1=∑i=1n−1∑k=1n−i2k+1≥∑i=1n−1∑k=1n−i22k≥∑i=1n−1Ω(lgn)=Ω(nlgn)(k≥1)
Some of above content refere to “Introduction to Algorithm”.
相关文章推荐
- 动易2006序列号破解算法公布
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法
- 基于C++实现的各种内部排序算法汇总
- C++线性时间的排序算法分析
- C++实现汉诺塔算法经典实例
- PHP实现克鲁斯卡尔算法实例解析
- C#获取关键字附近文字算法实例