计算机算法导论_第9章

计算机算法导论_第9章

ID:37924675

大小:1.03 MB

页数:39页

时间:2019-06-02

计算机算法导论_第9章_第1页
计算机算法导论_第9章_第2页
计算机算法导论_第9章_第3页
计算机算法导论_第9章_第4页
计算机算法导论_第9章_第5页
资源描述:

《计算机算法导论_第9章》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、IntroductiontoAlgorithms9MediansandOrderStatistics8/5/2021OrderStatisticsTheithorderstatisticinasetofnelementsistheithsmallestelementTheminimumisthusthe1storderstatisticThemaximumisthenthorderstatisticThemedianisthen/2orderstatisticIfniseven,thereare2medians8/5/2021SelectionProblemInput:

2、AsetAofn(distinct)numbersandanumberi,with1≤i≤n.Output:Theelementx∈Athatislargerthanexactlyi-1otherelementsofA.Howcanwecalculateorderstatistics?Whatistherunningtime?8/5/20219.1MinimumandMaximum8/5/2021MinimumandMaximumHowmanycomparisonsareneededtofindtheminimumelementinaset?Themaximum?MIN

3、IMUM(A)1min←A[1]2fori←2tolength[A]3doifmin>A[i]4thenmin←A[i]5returnmin8/5/2021SimultaneousMinimumandMaximumCanwefindtheminimumandmaximumwithlessthantwicethecost?Yes:WalkthroughelementsbypairsCompareeachelementinpairtotheotherComparethelargesttomaximum,smallesttominimumTotalcost:3comparis

4、onsper2elements≤3⌊n/2⌋=O(3n/2)8/5/20219.2Selectioninexpectedlineartime8/5/2021TheSelectionProblemAmoreinterestingproblemisselection:findingtheithsmallestelementofasetWewillshow:ApracticalrandomizedalgorithmwithO(n)expectedrunningtimeAcoolalgorithmoftheoreticalinterestonlywithO(n)worst-ca

5、serunningtime8/5/2021RandomizedSelectionKeyidea:usepartition()fromquicksortBut,onlyneedtoexamineonesubarrayThissavingsshowsupinrunningtime:O(n)Wewillagainuseaslightlydifferentpartitionq=RandomizedPartition(A,p,r)A[q]A[q]qpr8/5/2021RandomizedSelectionRandomizedSelect(A,p,r,i)if(p==r)the

6、nreturnA[p];q=RandomizedPartition(A,p,r)k=q-p+1;if(i==k)thenreturnA[q];if(i

7、ticseries)Nobetterthansorting!“Best”case:supposea9:1partitionT(n)=T(9n/10)+O(n)=???=O(n)(MasterTheorem,case3)Betterthansorting!Whatifthishadbeena99:1split?8/5/2021RandomizedSelection8/5/2021RandomizedSelection8/5/2021Calculatingexpectation8/5/2021Calculatingexpectation8/5

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。