算法分析
前言:开始对自己的学习做一个记录,看看自己的知识图谱是什么样子。如果可以帮助到其他人是也是一件非常开心的事,更多的是对学习的东西的一个整理。
学习地址:网易公开课的算法导论。链接:
http://open.163.com/special/opencourse/algorithms.html
QuickSort(快速排序)
·Dicide-And-Conquer
·Sort “in place”
·ver practical (with turnning)
1.Divide :Partition the input array into 2 subarray around an priot $elems n lower subarray <= x <= elems in upper subarray
2.Conquer : Recursively sort two subarrys
3.Combome : Tivial
key : Linear-time θ(n) partitioning subroution
partition(A,p,q) //A[p...q]
x<-A[p]
l<-p
for j<-p+1 to q
do if A[j] <= x
then i <- l+1
exch A[j] <-> A[j]
exch A[p] <-> A[i]
return i
Time = θ(n) for n-elem subarray
Ex 6,10,13,5,8,3,2,11 (x<-6)
6,5,3,10,8,13,2,11
6,5,3,2,10,8,13,11
6,5,3,2,10,8,13,11
2,5,3,6,10,8,13,11
QuickSort(A,p,q)
if p<q
then r<- Partition(A,p,q)
QuickSort(A,p,r-1)
QuickSort(A,r+1,q)
Init call QuickSort(A,1,n)
Analysis - assume all elems distinct
T(n) = warst-case time
· input sorted and reverse sorted
· one side of the partition of each partion has no elements
T(n) = T(n) + T(n-1) + θ(n)
= θ(1) + T(n-1) + θ(n)
=T(n-1) + θ(n)
=θ(n^2) ( arithmetic , like insertion sort)
//average case is good
Recursion Tree : T(n) = T(n) + T(n-1) +cn
T(n) = ···cn····························= cn
·········· /········ \·························/·······\
······T(0)·······T(n-1)··············T(0)·····c(n-1)
·························································/·······\
·······················································T(0)···T(n-1)
=······························ cn·······································
·····························/··········\··································
······················T(0)········c(n-1)·····························
·····································/·········\···························
································T(0)··········c(n-1)···················
·························································· 。 ···············
································································ 。·········
····································································θ(n)·····
c(n−1)+c(n−2)+…+θ(n)=∑k=1nck
T(n) = θ(n) + θ(n^2) = θ(n^2)
Best-cast analysis (intution only)
Partition splits array n/2…n/2
T(n) = 2T(n/2) + θ(n)=θ(nlgn)
split is always 1/10 : 9/10
T(n) = T(n/10) + T(9n/10) + θ(n)
T(n) = ···cn················································= ················cn·····················································
·········· /········ \············································/···············································\·····························
······T(n/10)·······T(9n/10)··················n/10·········································9n/10··················
···························································/·······\···········································/···········\···················
·······················································T(n/100)···T(9n/100)·······T(9n/100)···T(81n/100)
log10n/9<=T(n)
We alterrate spilt lucky,unlucky
L(n) = 2U(n/2) + θ(n) - - - - - - lucky
U(n) = 2L(n-1) + θ(n) - - - - - - unlucky
L(n) = 2(L(n/2 - 1) + θ(n/2)) + θ(n)
= 2L(n/2 - 1) + θ(n)
= θ(n)
Randomized quickSort(随机函数)
· running time is independent of the input ording
· no assumptions about the input distribution
· no specific input that can elict worst-case behavior
· worse-case is determined only by a random-number generator pivot on a random element
Analysis
T(N) = random version for running time assumiry rand #’s indep
For k = 0,1,2,3,..,n
f(x)={1,0,ifparttitiongeneratesak=n−k−1otherwise
E[Xk] = 0·Pr{Xk = 0} +1·Pr{Xk = 1}
=Pr{Xk = 1}
=1/n
T(n)=⎧⎩⎨⎪⎪⎪⎪⎪⎪⎪⎪T(0)+T(n−1)+θ(n)T(1)+T(n)+θ(n)T(n−1)+T(0)+θ(n)if0:n−1spiltif1:n−2spiltifn−1:0spilt
=∑k=0n−1(T(k)+T(n−k−1)+θ(n))
E(T(n))=∑k=0n−1E[Xk(T(k)+T(n−k−1)+θ(n))]
=∑k=0n−1E[Xk(T(k)]E[T(n−k−1)+θ(n))]
=∑k=0n−1E[Xk(T(k)]E[T(n−k−1)+θ(n))]
=1/n∑k=0n−1E[Xk(T(k)]E[T(n−k−1)+θ(n))]