版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
T&RTeamofAlgorithmDesignCollegeofComputerScienceandEngineering,CQUAlgorithmAnalysis&Design
IntroductiontoAlgorithmChapter8:LinearSortOutline8.1Howfastcanwesort?8.2CountingSort8.3RadixSort8.2BucketSort8.1Howfastcanwesort?Howfastcanwesort?Allthesortingalgorithmswehaveseensofararecomparisonsorts:usecomparisonstodeterminetherelativeorderofelements.E.g.,mergesort,quicksort,heapsort.Thebestworst-caserunningtimethatwe’veseenforcomparisonsortisO(n
logn).Actually,thisisindeedthebestworst-caserunningtimeofcomparisonsort.Decision-treeexampleSort<a1,a2,a3>Eachinternalnodeislabeledi:jfori,j∈{1,2,3}Theleftsub-treeshowssubsequentcomparisonswithai≤ajTherightsub-treeshowsubsequentcomparisonswithai>aj≤≤≤≤≤>>>>>Decision-treeexampleSort<a1,a2,a3>=
<9,4,6>Eachinternalnodeislabeledi:jfori,j∈{1,2,3}Theleftsub-treeshowssubsequentcomparisonswithai≤ajTherightsub-treeshowsubsequentcomparisonswithai>ajEachLeafdenotesapermutationoftheto-be-sortedarray≤≤≤≤≤>>>>>Decision-treeexampleSort<a1,a2,a3>=
<9,4,6>Eachinternalnodeislabeledi:jfori,j∈{1,2,3}Theleftsub-treeshowssubsequentcomparisonswithai≤ajTherightsub-treeshowsubsequentcomparisonswithai>ajEachLeafdenotesapermutationoftheto-be-sortedarray≤≤≤≤≤9>4>>>>Decision-treeexampleSort<a1,a2,a3>=
<9,4,6>Eachinternalnodeislabeledi:jfori,j∈{1,2,3}Theleftsub-treeshowssubsequentcomparisonswithai≤ajTherightsub-treeshowsubsequentcomparisonswithai>ajEachLeafdenotesapermutationoftheto-be-sortedarray≤≤≤≤≤9>4>>>9>6Decision-treeexampleSort<a1,a2,a3>=
<9,4,6>Eachinternalnodeislabeledi:jfori,j∈{1,2,3}Theleftsub-treeshowssubsequentcomparisonswithai≤ajTherightsub-treeshowsubsequentcomparisonswithai>ajEachLeafdenotesapermutationoftheto-be-sortedarray≤≤≤≤9>4>>>9>64≤6Decision-treeexampleSort<a1,a2,a3>=
<9,4,6>Eachinternalnodeislabeledi:jfori,j∈{1,2,3}Theleftsub-treeshowssubsequentcomparisonswithai≤ajTherightsub-treeshowsubsequentcomparisonswithai>ajEachLeafdenotesapermutationoftheto-be-sortedarray≤≤≤≤>>>9>49>64≤6Decision-treeexampleSort<a1,a2,a3>=
<9,4,6>Thetreecontainsthecomparisonsalongallpossiblepaths.Therunningtimeofthealgorithm=thelengthofthepathtaken.TheWorst-caserunningtime=theheightofthetree.≤≤≤≤>>>9>49>64≤6LowerBoundforComparisonSortingTheorem.AnycomparisonsortingalgorithmrequiresΩ(n
logn)comparisonsintheworstcaseProof:Worst-casedictatedbytreeheighthAnn-element-arrayhaven!differentpermutationsWeneedatlestoneleaftodenoteonepermutationThus,thelowerboundofh
canbedenotedby:h≥log(n!)≥log(n/e)n=nlogn-nloge=Ω(nlogn)Stirling’sApproximationHeapsortandmergesortareasymptoticallyoptimalcomparisonsortingalgorithms.CanWeSortFaster?Isthereafasteralgorithm?Canwesortinlineartime?HOW?8.2CountingSortCountingSortCountingsort:sortwithoutcomparisonInput:A[1..n],whereA[j]∈{1,2,…,k}Output:sortedarrayB[1..n]Auxiliarystorage:C[1..k]P.S.:NotethatthesizeoftheauxiliaryarrayisdecidedbythelargestelementinA.CountingSortForeachelementx
inA,ifthereare17elementslessthanxinA,thenx
deservetheoutputposition18.WhatifmultipleelementsequaltoxinA?Putthe1stinposition18,2ndinposition19,3rdinposition20,etc.Whatifthereare17elementsnotgreaterthanxinA?Putthelast
inposition17,thesecond-last
inposition16,etc.CountingSortPseudoCodeCounting-sortexample4134312345A:1234C:12345B:n=5,k=4Counting-sortexample4134312345A:1234C:12345B:Counting-sortexample4134312345A:00001234C:12345B:Counting-sortexample4134312345A:00001234C:12345B:Counting-sortexample03431412345A:10001234C:12345B:Counting-sortexample0113431412345A:001234C:12345B:Counting-sortexample01113431412345A:01234C:12345B:Counting-sortexample11123431412345A:01234C:12345B:Counting-sortexample21123431412345A:01234C:12345B:Counting-sortexample21123431412345A:01234C:12345B:Counting-sortexample022103431412345A:1234C:12345B:Counting-sortexample022113431412345A:1234C:12345B:Counting-sortexample122133431412345A:1234C:12345B:Counting-sortexample123153431412345A:1234C:12345B:Counting-sortexample51133431412345A:11234C:12345B:Counting-sortexample51133431412345A:11234C:12345B:Counting-sortexample333513431412345A:11234C:12345B:Counting-sortexample332513431412345A:11234C:12345B:Counting-sortexample51123431412345A:11234C:312345B:Counting-sortexample5342513431412345A:11234C:12345B:Counting-sortexample5434213431412345A:11234C:12345B:Counting-sortexample41123431412345A:11234C:4312345B:Counting-sortexample44332213431412345A:11234C:12345B:Counting-sortexample44332113431412345A:11234C:12345B:Counting-sortexample41113431412345A:11234C:43312345B:Counting-sortexample134431113431412345A:11234C:12345B:Counting-sortexample103443113431412345A:11234C:12345B:Counting-sortexample41013431412345A:11234C:433112345B:Counting-sortexample1034434143431412345A:11234C:12345B:Counting-sortexample4310343413431412345A:11234C:12345B:Counting-sortexample31013431412345A:11234C:4433112345B:AnalyzingCountingSortTheoveralltimeisO(k+n)50O(k)O(n)O(k)O(n)RunningtimeofCountingSortIfk=O(n),thencountingsorttakesO(n)time.However,comparisonsorttakeΩ(n
logn)time!Whatenablesthislinearimplementation?CountingsortisnotacomparisonsortOr,singlecomparisonsbetweenelementsneveroccurs51StabilityofCountingSortCountingsortisstable:itreservestheinputorderamongequalelements.Exercise:whatisthelimitationofcountingsort?528.3RadixSortRadixSortOrigin:HermanHollerith’scard-sortingmachineforthe1890U.S.CensusThe1880U.S.Censustookalmost
10yearstoprocess.WhilealectureratMIT,Hollerith
prototypedpunched-cardtechnology.Hismachines,includinga“cardsorter”,
allowedthe1890censustotaltobe
reportedin6weeks.HefoundedtheTabulatingMachine
Companyin1911,whichmerged
withothercompaniesin1924
toformIBM.54OperationofRadixSort559779605977960534684732553325734463825355230567799StablesortOperationofRadixSort5697796059779605346847325533257344638253552305677992535523734834622335550969577StablesortStablesortOperationofRadixSort57977960597796053468473255332573446382535523056779925355237348346223355509695777348346334467825355239567709StablesortStablesortStablesortCorrectnessofRadixSort587348346223355509695773344678253552395677091.Basedontheinductionon
digitposition.2.Assumethatthenumbersaresortedbytheirlow-ordert–1digits.3.Andthensortondigitt.StablesortCorrectnessofRadixSort59734834622335550969577334467825355239567709StablesortTwonumbersthatdifferindigittarecorrectlysorted.1.Basedontheinductionon
digitposition.2.Assumethatthenumbersaresortedbytheirlow-ordert–1digits.3.Andthensortondigitt.CorrectnessofRadixSort60734834622335550969577334467825355239567709StablesortTwonumbersequalindigittareputinthesameorderastheinput:correctorder1.Basedontheinductionon
digitposition.2.Assumethatthenumbersaresortedbytheirlow-ordert–1digits.3.Andthensortondigitt.AnalyzingRadixSortO(d
T(n))whereT(n)denotestherunningtimeoftheinnersort.Ifweusecountingsortastheinnersortanddisconstant,thenradixsortcanachieveO(n)LSD:sortnumbersontheirLeastSignificantDigitfirst.RADIX-SORTRADIX-SORT(A,d)1
for
i
←1
to
d2do
useastablesorttosortarrayAondigit
i8.4BucketSortBucketSortSimilartocountingsort,bucketsortisfastbecauseofaninitialhypothesisDifferentintuitivehypothesis.CountingSort:theinputconsistofintegersinasmallrangeBucketSort:theinputkeeptoauniformdistributionGeneralIdea:dividetheinterval[0,1)intonequal-sizedsubintervalswhichformtheso-calledbuckets,andthendistributetheninputnumberintothebuckets.63BucketSortAssumethatalltheinputisgeneratedbyarandomprocessthatdistributeselementsuniformovertheinterval[0,1)Applybucketsort:①Allocateabucketforeachvalueofthekey>>Thatis,insertA[i]intolistB[?n
A[i]?]②Foreachbucket>>sortlistB[i]withinsertionsort③concatenatethelistB[0],B[1],…,B[n-1]togetherinorder64BucketSortPseudoCode650BucketSortExample(a)TheinputarrayA[1..10](b)ThearrayB[0..9]ofsortedlists(b
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 某著名企業(yè)績效管理培訓(xùn)0704
- 《GBT 17507-2008透射電子顯微鏡X射線能譜分析生物薄標(biāo)樣的通 用技術(shù)條件》專題研究報(bào)告深度
- 《GBT 5296.7-2008消費(fèi)品使用說明 第7部分:體育器材》專題研究報(bào)告
- 《FZT 99020-2018針織圓緯機(jī)數(shù)控系統(tǒng)通 用技術(shù)規(guī)范》專題研究報(bào)告
- 《FZT 64059-2016 機(jī)織拉毛粘合襯》專題研究報(bào)告
- 道路保潔安全培訓(xùn)
- 2024毛發(fā)移植圍手術(shù)期提高毛囊成活率的專家共識(shí)
- 達(dá)美樂課件培訓(xùn)
- 邊坡防護(hù)工程安全培訓(xùn)課件
- 車隊(duì)管理安全培訓(xùn)任務(wù)課件
- 2025年河南農(nóng)業(yè)大學(xué)馬克思主義基本原理概論期末考試真題匯編
- 2025年國企副總經(jīng)理年終述職報(bào)告
- 昆山鈔票紙業(yè)有限公司2026年度招聘備考題庫及一套答案詳解
- 施工消防安全評估措施
- 高考語文復(fù)習(xí)古代詩歌形象鑒賞課件
- 2025中國醫(yī)學(xué)科學(xué)院北京協(xié)和醫(yī)學(xué)院勞務(wù)派遣制工作人員招聘3人筆試備考重點(diǎn)試題及答案解析
- 區(qū)域創(chuàng)新一體化機(jī)制-洞察及研究
- 兒科健康評估與護(hù)理
- 四診合參在護(hù)理評估中的綜合應(yīng)用
- 2026年青海省交通控股集團(tuán)有限公司招聘(45人)筆試考試參考題庫及答案解析
- GB 46768-2025有限空間作業(yè)安全技術(shù)規(guī)范
評論
0/150
提交評論