第七章 快速排序_第1頁
第七章 快速排序_第2頁
第七章 快速排序_第3頁
第七章 快速排序_第4頁
第七章 快速排序_第5頁
已閱讀5頁,還剩15頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論