算法分析(三)

本文详细介绍了快速排序算法的原理与实现过程,包括分治策略、原地排序特性及其实用性。通过对快速排序的递归过程进行分析,展示了其在最坏情况、平均情况及最好情况下的时间复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

算法分析

前言:开始对自己的学习做一个记录,看看自己的知识图谱是什么样子。如果可以帮助到其他人是也是一件非常开心的事,更多的是对学习的东西的一个整理。

学习地址:网易公开课的算法导论。链接:
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(n1)+c(n2)++θ(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)

θ(n)

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=nk1otherwise

E[Xk] = 0·Pr{Xk = 0} +1·Pr{Xk = 1}
=Pr{Xk = 1}
=1/n

T(n)=T(0)+T(n1)+θ(n)T(1)+T(n)+θ(n)T(n1)+T(0)+θ(n)if0:n1spiltif1:n2spiltifn1:0spilt

=k=0n1(T(k)+T(nk1)+θ(n))

E(T(n))=k=0n1E[Xk(T(k)+T(nk1)+θ(n))]

=k=0n1E[Xk(T(k)]E[T(nk1)+θ(n))]

=k=0n1E[Xk(T(k)]E[T(nk1)+θ(n))]

=1/nk=0n1E[Xk(T(k)]E[T(nk1)+θ(n))]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值