版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Quicksort(快速排序)
(一种算法设计技术)主要内容
Quick
sort快速排序Randomizedquicksortalgorithm随机版本快速排序算法Quicksort快速排序
Efficientsortingalgorithm--ProposedbyC.A.R.Hoarein1962--Divide-and-Conqueralgorithm--Sorts“inplace”(likeinsertionsort,butnotlikemergesort)--Verypractical(withtuning)--CanbeviewedasarandomizedLasVegasalgorithm--Worst-caserunningtime:Θ(n2)--Expectedrunningtime:Θ(nlogn)--ConstanthiddeninΘ(nlogn)aresmall.Quicksort’sidea--DivideandConquer
Quicksortann-elementarray:--Divide:Partitionthearrayintotwosubarraysaroundapivotxsuchthatelementsinlowersubarray≤x≤elementsinuppersubarray
--Conquer:Recursivelysortthesubarrays.--Combine:Trivial.
Keypoint:Linear-timepartitioningsubroutine.Quicksort’sidea--DivideandConquerInitialcallQuicksort(A,1,n)Partitioningsubroutine划分子程序A[p,…,i]:knowntobe≤pivotA[i+1,…,j-1]:knowntobe>pivotA[j…r-1]:notyetexaminedA[r]:pivotExampleofPartitioningTheoperationofPartitiononasamplearray.Lightlyshadedarrayelementsareallwithvaluesnogreaterthanx(thepivot).Heavilyshadedarrayelementsareallwithvaluesgreaterthanx.Analysisofquicksort
AssumeallinputelementsaredistinctInpractice,therearebetterpartitioningalgorithmsforwhenduplicateinputelementsmayexistLetT(n)=worst-caserunningtimeonanarrayofnelementsWorst-caseofquicksort
Inputsortedorreversesorted已有序PartitionaroundminormaxelementOnesideofpartitionalwayshasnoelements
T(n)=T(n-1)+T(0)+Θ(n)=T(n-1)+Θ(1)+Θ(n)=T(n-1)+Θ(n)=Θ(n2)(算法级数)Worst-caserecursivetree
T(n)=T(n-1)+T(0)+Θ(n)
ArecursiontreeforQUICKSORTinwhichthePartitionprocedurealwaysputsonlyasingleelementononesideofthepartition(theworstcase).Theresultingrunningtimeis
Θ(n2)BestCasePartitioning对等划分
ArecursiontreeforQUICKSORTinwhichthePartitionalwaysbalancesthetwosidesofthepartitionequally(thebestcase).Theresultingrunningtimeis
Θ(nlgn)Θ(nlgn)Best-caseanalysis
Ifwe’relucky,PARTITIONsplitsthearrayevenly:--T(n)=2T(n/2)+Θ(n)=Θ(nlogn).Whatifthesplitisalways1/10:9/10?平衡划分
--T(n)
≤T(9n/10)+T(n/10)+Θ(n)--Whatisthesolutiontothisrecurrence?ARecursiontree
ArecursiontreeforQUICKSORTinwhichPARTITIONalwaysproducesa9-to-1split,yieldingarunningtimeofO(n
lgn)Worstcase
Θ(n2),whywepreferQuicksorttoMergesortQuicksortVS.MergesortWhichisbetter?Testenvironment:IBM370/158ProgramminginPL/1Inputconsistsofrandomintegersrangein(0,10000)n10001500200025003000350040004500MERGESORT500750105014001650200022502650QUICKSORT40060085010501300155018002050n50005500600065007000750080008500MERGESORT29003450350038504250455049505200QUICKSORT23002650280030003350370039004100Quicksort平均时间分析Quicksort平均时间分析Quicksort平均时间分析Randomize-PartitioningRandomizedquicks
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 上港集团三副岗位技能考试题集含答案
- 2026年河南经贸职业学院单招职业倾向性考试题库附答案解析
- 2024年晋宁县招教考试备考题库及答案1套
- TCL科技术支持工程师产品知识竞赛题库含答案
- 2026年理财规划师之三级理财规划师考试题库500道及参考答案【综合卷】
- 2024年炎黄职业技术学院辅导员招聘考试真题汇编附答案
- 2026年一级注册建筑师之建筑设计考试题库500道及完整答案【名师系列】
- 2026年湖北城市建设职业技术学院单招职业技能测试模拟测试卷附答案解析
- 2026年中级银行从业资格之中级公司信贷考试题库300道附答案【能力提升】
- 2026年设备监理师考试题库500道及完整答案(历年真题)
- 2025年看守所民警述职报告
- 景区接待员工培训课件
- 客源国概况日本
- 学位授予点评估汇报
- 《Stata数据统计分析教程》
- 2024-2025学年广州市越秀区八年级上学期期末语文试卷(含答案)
- 宠物诊疗治疗试卷2025真题
- 媒体市场竞争力分析-洞察及研究
- 口腔科口腔溃疡患者漱口液选择建议
- 精神科抑郁症心理干预培训方案
- 2025年国家开放大学(电大)《外国文学》期末考试复习题库及答案解析
评论
0/150
提交评论