You are on page 1of 24

# 1

QuickSort
Tounderstandquicksort,letslookatahighleveldescriptionofthe
algorithm
1)Divide:IfthesequenceShas2ormoreelements,selectanelementx
fromStobeyourpivot.Anyarbitraryelement,likethelast,willdo.
RemovealltheelementsofSanddividetheminto3sequences:
L,holdsSselementslessthanx
E,holdsSselementsequaltox
G,holdsSselementsgreaterthanx

2)Recurse:RecursivelysortLandG
3)Conquer:Finally,toputelementsbackintoSinorder,firstinserts
theelementsofL,thenthoseofE,andthoseofG.
Herearesomeprettydiagrams....

IdeaofQuickSort
1)Select:pickanelement

2)Divide:rearrangeelementsso
thatxgoestoitsfinalpositionE
3)RecurseandConquer:
recursivelysort

QuickSortTree

QuickSortTree

QuickSortTree

QuickSortTree

QuickSortTree

QuickSortTree

QuickSortTree

10

QuickSortTree

11

QuickSortTree

12

QuickSortTree

13

QuickSortTree

14

QuickSortTree

Skipping...

15

...Finally

16

InPlaceQuickSort
Dividestep:lscansthesequencefromtheleft,andrfromtheright.

Aswapisperformedwhenlisatanelementlargerthanthepivotand
risatonesmallerthanthepivot.

17

InPlaceQuickSort(contd.)

Afinalswapwiththepivotcompletesthedividestep

18

InPlaceQuickSortcode
publicclassArrayQuickSortimplementsSortObject{
publicvoidsort(SequenceS,Comparatorc){
quicksort(S,C,0,S.size()1);
}privatevoidquicksort(SequenceS,Comparatorc,intleftBound,
intrightBound){//leftandrightmostranksofsortingrange
if(S.size()<2)return;//asequencewith0or1elements
if(leftBound>=rightBound)return;//terminaterecursion
//pickthepivotasthecurrentlastelementinrange
Objectpivot=S.atRank(rightBound).element();
//indicesusedtoscanthesortingrange
intleftIndex=leftBound;//willscanrightward
intrightIndex=rightBound1;//willscanleftward

19

InPlaceQuickSortcode(contd.)

//outerloop
while(leftIndex<=rightIndex){
//scanrightwarduntilanelementlargerthan
//thepivotisfoundortheindicescross
while((leftIndex<=rightIndex)&&(c.isLessThanOrEqualTo
(S.atRank(leftIndex).element(),pivot))
leftIndex++;
//scanleftwarduntilanelementsmallerthan
//thepivotisfoundortheindicescross
while(rightIndex>=leftIndex)&&(c.isGreaterThanOrEqualTo
(S.atRank(rightIndex).element(),pivot))
rightIndex;
//ifanelementlargerthanthepivotandan
//elementsmallerthanthepivothavebeen
//found,swapthem
if(leftIndex<rightIndex)
S.swap(S.atRank(leftIndex),S.atRank(rightIndex));
}
//theouterloopcontinuesuntil

//theindicescross.Endofouterloop.

20

InPlaceQuickSortcode(contd.)
//putthepivotinitsplacebyswappingit
//withtheelementatleftIndex
S.swap(S.atRank(leftIndex),S.atRank(rightBound));
//thepivotisnowatleftIndex,sorecur
//onbothsides
quicksort(S,c,leftBound,leftIndex1);
quickSort(S,c,leftIndex+1,rightBound);
}//endquicksortmethod
}//endArrayQuickSortclass

21

AnalysisofRunningTime
ConsideraquicksorttreeT:
Letsi(n)denotethesumoftheinputsizesofthenodesatdepthiinT.

Weknowthats0(n)=nsincetherootofTisassociatedwiththeentire
inputset.
Also,s1(n)=n1sincethepivotisnotpropagated.
Thus:eithers2(n)=n3,orn2(ifoneofthenodeshasazeroinput
size).
Theworstcaserunningtimeofaquicksortisthen:
Thusquick
sortrunsin
timeO(n2)in
theworst
case

22

AnalysisofRunningTime
(contd.)
Nowtolookatthebestcaserunningtime:
Wecanseethatquicksortbehavesoptimallyif,wheneverasequence
SisdividedintosubsequencesLandG,theyareofequalsize.
Moreprecisely:
s0(n)=n
s1(n)=n1
s2(n)=n(1+2)=n3
s3(n)=n(1+2+22)=n7

si(n)=n(1+2+22+...+2i1)=n2i1
...

ThisimpliesthatThasheightO(logn)
BestCaseTimeComplexity:O(nlogn)

23

RandomizedQuickSort
Selectthepivotasarandomelementofthesequence.
Theexpectedrunningtimeofrandomizedquicksortonasequenceof
sizenisO(nlogn).
ThetimespentatalevelofthequicksorttreeisO(n)
WeshowthattheexpectedheightofthequicksorttreeisO(logn)