您的位置:首页 > 产品设计 > UI/UE

Quick Sort

2016-02-25 00:37 288 查看

QuickSort-Tony Hoare 1962

Divide and Conquer

Sorts”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/2

T(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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: