Tải bản đầy đủ - 0 (trang)
7 Comparison of Mergesort, Quicksort, and Heapsort

7 Comparison of Mergesort, Quicksort, and Heapsort

Tải bản đầy đủ - 0trang

[*]Entriesareapproximate;theaveragecasesforMergesortand

Heapsortareslightlybetterthantheworstcases.tIfitisrequiredthat

therecordsbeinsortedsequenceincontiguousarrayslots,theworst

caseisinΘ(n).

[†]Ifitisrequiredthattherecordsbeinsortedsequenceincontiguous

arrayslots,theworstcaseisinΘ(n).



7.8LowerBoundsforSortingOnlyby

ComparisonofKeys

WehavedevelopedΘ(nlgn)sortingalgorithms,whichrepresenta

substantialimprovementoverquadratic-timealgorithms.Agoodquestion

iswhetherwecandevelopsortingalgorithmswhosetimecomplexities

areofevenbetterorder.Weshowthat,aslongaswelimitourselvesto

sortingonlybycomparisonsofkeys,suchalgorithmsarenotpossible.

Althoughourresultsstillholdifweconsiderprobabilisticsorting

algorithms,weobtaintheresultsfordeterministicsortingalgorithms.(See

Section5.3foradiscussionofprobabilisticanddeterministicalgorithms.)

AsdoneinSection7.3,weobtainourresultsundertheassumptionthat

thenkeysaredistinct.Furthermore,asdiscussedinthatsection,wecan

assumethatthenkeysaresimplythepositiveintegers1,2,…,n,

becausewecansubstitute1forthesmallestkey,2forthesecond

smallest,andsoon.



7.8.1DecisionTreesforSortingAlgorithms



Considerthefollowingalgorithmforsortingthreekeys.

voidsortthree(keytypeS[])

{

keytypea,b,c;

a=S[1];b=S[2];c=S[3];

if(a
if(b
S=a,b,c;//ThismeansS[1]=

elseif(a
S=a,c,b;

else

S=c,a,b;

elseif(b
if(a
S=b,a,c;

else

S=b,c,a;

else

S=c,b,a;

}

Wecanassociateabinarytreewithproceduresortthreeasfollows,We

placethecomparisonofaandbattheroot.Theleftchildoftheroot

containsthecomparisonthatismadeifa
containsthecomparisonthatismadeifa≤b.Weproceeddownward,

creatingnodesinthetreeuntilallpossiblecomparisonsdonebythe

algorithmareassignednodes.Thesortedkeysarestoredattheleaves.

Figure7.11showstheentiretree.Thistreeiscalledadecisiontree

becauseateachnodeadecisionmustbemadeastowhichnodetovisit

next.Theactionofproceduresortthreeonaparticularinputcorresponds

tofollowingtheuniquepathfromtheroottoaleaf,determinedbythat

input.Thereisaleafinthetreeforeverypermutationofthreekeys,

becausethealgorithmcansorteverypossibleinputofsize3.





Figure7.11:Thedecisiontreecorrespondingtoprocedure

sortthree.

Adecisiontreeiscalledvalidforsortingnkeysif,foreachpermutationof

thenkeys,thereisapathfromtheroottoaleafthatsortsthat

permutation.Thatis,itcansorteveryinputofsizen.Forexample,the

decisiontreeinFigure7.11isvalidforsortingthreekeys,butwouldno

longerbevalidifweremovedanybranchfromthetree.Toevery

deterministicalgorithmforsortingnkeys,therecorrespondsatleastone

validdecisiontree.ThedecisiontreeinFigure7.11correspondsto

proceduresortthree,andthedecisiontreeinFigure7.12correspondsto

ExchangeSortwhensortingthreekeys.(Youareencouragedtoverify

this.)Inthattree,a,b,andcareagaintheinitialvaluesofS[1],S[2],and

S[3].Whenanodecontains,forexample,thecomparison"c
doesnotmeanthatExchangeSortcomparesS[3]withS[2]atthat

point;rather,itmeansthatExchangeSortcomparesthearrayitem

whosecurrentvalueiscwiththeonewhosecurrentvalueisb.Inthetree

inFigure7.12,noticethatthelevel–2nodecontainingthecomparison"b


comparison,contradictstheanswersobtainedonthepathleadingtothat

node,whichmeansthatitsrightchildcouldnotbereachedbymakinga

consistentsequenceofdecisionsstartingattheroot.ExchangeSort

makesanunnecessarycomparisonatthis





Figure7.12:ThedecisiontreecorrespondingtoExchangeSortwhen

sortingthreekeys.





Figure7.13:Thetreesin(a)and(b)havethesamenumberofleaves,

butthetreein(b)hasasmallerEPL

point,becauseExchangeSortdoesnot"know"thattheanswertothe

questionmustbe"yes."Thisoftenhappensinsuboptimalsorting

algorithms.Wesaythatadecisiontreeisprunedifeveryleafcanbe

reachedfromtherootbymakingaconsistentsequenceofdecisions.The

decisiontreeinFigure7.12ispruned,whereasitwouldnotbeprunedif

weaddedarightchildtothenodejustdiscussed,eventhoughitwould

stillbevalidandwouldstillcorrespondtoExchangeSort.Clearly,to

everydeterministicalgorithmforsortingnkeystherecorrespondsa

pruned,validdecisiontree.Therefore,wehavethefollowinglemma.



Lemma7.1

Toeverydeterministicalgorithmforsortingndistinctkeysthere

correspondsapruned,valid,binarydecisiontreecontainingexactlyn!

leaves.

Proof:Asjustmentioned,thereisapruned,validdecisiontree

correspondingtoanyalgorithmforsortingnkeys.Whenallthekeysare

distinct,theresultofacomparisonisalways"<"or">"Therefore,each

nodeinthattreehasatmosttwochildren,whichmeansthatitisabinary

tree.Nextweshowthatithasn!leaves.Becausetherearen!different

inputsthatcontainndistinctkeysandbecauseadecisiontreeisvalidfor

sortingndistinctkeysonlyifithasaleafforeveryinput,thetreehasat

leastn!leaves.Becausethereisauniquepathinthetreeforeachofthe

n!differentinputsandbecauseeveryleafinapruneddecisiontreemust

bereachable,thetreecanhavenomorethann!leaves.Therefore,the

treehasexactlyn!leaves.



UsingLemma7.1,wecandetermineboundsforsortingndistinctkeysby

investigatingbinarytreeswith!leaves.Wedothisnext.



7.8.2LowerBoundsforWorst-CaseBehavior

Toobtainaboundfortheworst-casenumberofcomparisonsofkeys,we

needthefollowinglemma.

Lemma7.2

Theworst-casenumberofcomparisonsdonebyadecisiontreeisequal

toitsdepth.

Proof:Givensomeinput,thenumberofcomparisonsdonebyadecision

treeisthenumberofinternalnodesonthepathfollowedforthatinput.

Thenumberofinternalnodesisthesameasthelengthofthepath.

Therefore,theworstcasenumberofcomparisonsdonebyadecisiontree

isthelengthofthelongestpathtoaleaf,whichisthedepthofthe

decisiontree.



ByLemmas7.1and7.2,weneedonlyfindalowerboundonthedepthof

abinarytreecontainingn!leavestoobtainourlowerboundfortheworstcasebehavior.Therequiredlowerboundondepthisfoundbymeansof

thefollowinglemmasandtheorems.

Lemma7.3

Ifmisthenumberofleavesinabinarytreeanddisthedepth,then



Proof:Usinginductionond,weshowfirstthat



Inductionbase:Abinarytreewithdepth0hasonenodethatisboththe

rootandtheonlyleaf.Therefore,forsuchatree,thenumberofleavesm

equals1,and



Inductionhypothesis:Assumethat,foranybinarytreewithdepthd,



wheremisthenumberofleaves.

Inductionstep:Weneedtoshowthat,foranybinarytreewithdepthd+

1,



wherem1isthenumberofleaves.Ifweremovealltheleavesfromsuch

atree,wehaveatreewithdepthdwhoseleavesaretheparentsofthe

leavesinouroriginaltree.Ifmisthenumberoftheseparents,then,by

theinductionhypothesis,



Becauseeachparentcanhaveatmosttwochildren,



Combiningtheselasttwoinequalitiesyields



whichcompletestheinductionproof.

TakingthelgofbothsidesofInequality7.1yields



Becausedisaninteger,thisimplies



Theorem7.2



Anydeterministicalgorithmthatsortsndistinctkeysonlybycomparisons

ofkeysmustintheworstcasedoatleast



Proof:ByLemma7.1,toanysuchalgorithmtherecorrespondsapruned,

valid,binarydecisiontreecontainingn!leaves.ByLemma7.3,thedepth

ofthattreeisgreaterthanorequalto[lg(n!)].Thetheoremnowfollows,

becauseLemma7.2saysthatanydecisiontree'sworst-casenumberof

comparisonsisgivenbyitsdepth.



Howdoesthisboundcomparewiththeworst-caseperformanceof

Mergesort—namely,nlgn–(n–1)?Lemma7.4enablesustocompare

thetwo.

Lemma7.4

Foranypositiveintegern,



Proof:Theproofrequiresknowledgeofintegralcalculus.Wehave



Theorem7.3

Anydeterministicalgorithmthatsortsndistinctkeysonlybycomparisons

ofkeysmustintheworstcasedoatleast



Proof:TheprooffollowsfromTheorem7.2andLemma7.4.



WeseethatMergesort'sworst-caseperformanceofnlgn–(n–1)is

closetooptimal.Nextweshowthatthisalsoholdsforitsaverage-case

performance.



7.8.3LowerBoundsforAverage-CaseBehavior

Weobtainourresultsundertheassumptionthatallpossible

permutationsareequallylikelytobetheinput.

Ifthepruned,valid,binarydecisiontreecorrespondingtoadeterministic

sortingalgorithmforsortingndistinctkeyscontainsanycomparison

nodeswithonlyonechild(asisthecaseforthetreeinFigure7.12),we

canreplaceeachsuchnodebyitschildandprunethechildtoobtaina

decisiontreethatsortsusingnomorecomparisonsthandidtheoriginal

tree.Everynonleafinthenewtreewillcontainexactlytwochildren.A

binarytreeinwhicheverynonleafcontainsexactlytwochildreniscalled

a2-tree.Wesummarizethisresultwiththefollowinglemma.

Lemma7.5

Toeverypruned,valid,binarydecisiontreeforsortingndistinctkeys,

therecorrespondsapruned,validdecision2-treethatisatleastas

efficientastheoriginaltree.

Proof:theprooffollowsfromtheprecedingdiscussion.



Theexternalpathlength(EPL)ofatreeisthetotallengthofallpaths

fromtheroottotheleaves.Forexample,forthetreeinFigure7.11,



Recallthatthenumberofcomparisonsdonebyadecisiontreetoreacha

leafisthelengthofthepathtotheleaf.Therefore,theEPLofadecision

treeisthetotalnumberofcomparisonsdonebythedecisiontreetosort

allpossibleinputs.Becausetherearen!differentinputsofsizen(when

allkeysaredistinct)andbecauseweareassumingallinputstobe

equallylikely,theaveragenumberofcomparisonsdonebyadecision

treeforsortingndistinctkeysisgivenby



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

7 Comparison of Mergesort, Quicksort, and Heapsort

Tải bản đầy đủ ngay(0 tr)

×