Quick Sort
2016-02-25 00:37
288 查看
QuickSort-Tony Hoare 1962
Divide and ConquerSorts”in place”
Very practical(with tuning)
Divide and conquer
1. Divide :partition array into 2 sub-arrays around pivot x such that elements in the lower sub-array ≤x≤\le x \leelements in upper sub-array.
2. Conquer :Recursively sort 2 sub-arrays.
3. Combine :Trivial
Key:Linear-time (Θ(n)\Theta(n)) partitioning subroutine
T(n)=Θ(n)\Theta(n)for n elements sub-array
Example:
Program:
Analysis-assume all elements distinct.
Worst case analysis
input sorted or reverse sorted.One side of partition has no elements.T(n)=T(0)+T(n−1)+Θ(n)=Θ(1)+Θ(n)+T(n−1)=Θ(n)+T(n−1)=Θ(n2)T(n)=T(0)+T(n-1)+\Theta(n)=\Theta(1)+\Theta(n)+T(n-1)=\Theta(n)+T(n-1)=\Theta(n^2)
(arithmetic series,like insertion sort)
Best case analysis(intuition only)
If we are really lucky,partition slits the array n/2 :n/2T(n)=2T(n/2)+Θ(n)=Θ(nlog2n)T(n)=2T(n/2)+\Theta(n)=\Theta(nlog_2n)
Suppose: split is always 1/10:9/10
T(n)=T(n/10)+T(9n/10)+Θ(n)T(n)=T(n/10)+T(9n/10)+\Theta(n)
log109n=log2nlog2109log_{ \frac{10}{9}}n= \frac{log_2n}{log_2\frac{10}{9}}
log10n=log2nlog210log_{10}n= \frac{log_2n}{log_2{10}}
So T(n)=Θ(nlog2n)T(n)=\Theta(nlog_2n)
Suppose:we alternate lucky,unlucky,lucky,unlucky…
lucky : L(n)=2U((n/2)+Θ(n)L(n)=2U((n/2)+\Theta(n)
unlucky : u(n)=L(n−1)+Θ(n)u(n)=L(n-1)+\Theta(n)
Then: L(n)=2(L(n2−1)+Θ(n/2))+Θ(n)=2L(n2−1)+Θ(n)=Θ(nlog2n)L(n)=2(L(\frac{n}{2}-1)+\Theta(n/2))+\Theta(n)=2L(\frac{n}{2}-1)+\Theta(n)=\Theta(nlog_2n)
To keep us usually lucky
1. Randomly range the elements.
2. Randomly choose the pivot.
Randomized QuickSort
running time is independent of input ordering.no assumptions about input distribution.
no specific input elicit worst-case behavior.
worst-case determined only by a random number generator.
pivot on a random element
Analysis
Let T(n) be the random variable for running time assuming the random numbers are independent.Absorb k=,1 terms into Θ(n)\Theta(n) for technique convenience.
E[T(n)]=2n∑n−1k=2(E[T(k)]+Θ(n))E[T(n)]=\frac{2}{n}\sum_{k=2}^{n-1}(E[T(k)]+\Theta(n))
Prove E[T(n)]≤anlog2nE[T(n)]\le anlog_2n for constant a >0
Choose a big enough so that E[T(n)]≤anlog2nE[T(n)]\le anlog_2n for small n.
Use a fact:∑n−1k=2klog2k≤12n2log2n−18n2\sum_{k=2}^{n-1}klog_2k\le \frac{1}{2}n^2log_2n-\frac{1}{8}n^2
substitution:
E[T(n)]≤n2∑n−1k=2(aklog2k+Θ(n))≤2n∗(a2n2log2n−18n2)+Θ(n)=anlog2n−(an4−Θ(n))≤anlog2nE[T(n)]\le \frac{n}{2}\sum_{k=2}^{n-1}(aklog_2k+\Theta(n))\le \frac{2}{n}*(\frac{a}{2}n^2log_2n-\frac{1}{8}n^2)+\Theta(n)=anlog_2n-(\frac{an}{4}-\Theta(n))\le anlog_2n
if a is big enough so that an/4 dominates Θ(n)\Theta(n).
It is typically three or more times faster than Merge Sort.
It tends to work well with cashes in virtual memory.
Code for Quick Sort:
Quick Sort code in github
相关文章推荐
- [Locked] Binary Tree Longest Consecutive Sequence
- Arduino、arm、树莓派、单片机四者有什么不同?
- C# 集合类 :(Array、 Arraylist、List、Hashtable、Dictionary、Stack、Queue)
- 浅谈 easyui tabs 的href和content属性
- QSqlQuery::value: not positioned on a valid record
- Java break continue return 的区别
- easyUI 对话框的关闭事件
- UITableView 小节-备
- GitLab: Failed to authorize your Git request: inte
- [leetcode] Divide and Conquer
- iOS UITableView的使用大全-备用
- 设计模式:Builder模式(多个构造器参数时可显著改善可读性)
- dialog子线程操作UI异常:Only the original thread that created a view hierarchy can touch its views
- dialog子线程操作UI异常:Only the original thread that created a view hierarchy can touch its views
- WeUI 入门教程
- 传智播客168期JavaEE就业班(第十天 request response)
- ORA-02287:此处不允许序号(sequence number not allowed here) 的避免以及强制实现
- UIAutomator定位Android控件的方法实践和建议
- Correlated Subqueries 相关子查询
- iOS 用QuickLook浏览文件和图片