lecture 6 - stanford universityweb.stanford.edu/.../lecture6/lecture6-compressed.pdflecture 6...

42
Lecture 6 Sorting lower bounds and O(n)-time sorting

Upload: others

Post on 27-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Lecture6SortinglowerboundsandO(n)-timesorting

Page 2: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Announcements

• HW2dueFriday

• HW3postedFriday

• PleasesendanyOAEletterstoJessicaSu(jtysu)ASAP.

Page 3: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Sorting

• We’veseenafewO(nlog(n))-time algorithms.

• MERGESORThasworst-caserunningtimeO(nlog(n))

• QUICKSORThasexpectedrunningtimeO(nlog(n))

Canwedobetter?

Dependsonwho

youask…

Page 4: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

AnO(1)-timealgorithmforsorting:

StickSort

• Problem:sortthesensticksbylength.

• Algorithm:• Dropthemonatable.

• Nowtheyaresortedthisway.

Page 5: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Thatmayhavebeenunsatisfying

• ButStickSort doesraisesomeimportantquestions:

• Whatisourmodelofcomputation?

• Input: array

• Output: sortedarray

• Operationsallowed:comparisons

-vs-

• Input:sticks

• Output:sortedsticksinverticalorder

• Operationsallowed:droppingontables

• Whatarereasonablemodelsofcomputation?

Page 6: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Today:two(more)models

• Comparison-basedsortingmodel

• ThisincludesMergeSort,QuickSort,InsertionSort

• We’llseethatany algorithminthismodelmusttakeatleastΩ(nlog(n)) steps.

• Anothermodel(morereasonablethanthestickmodel…)

• BucketSort andRadixSort

• BothrunintimeO(n)

Page 7: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Comparison-basedsorting

Page 8: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Comparison-basedsortingalgorithms

Thereisagenie whoknowswhat

therightorderis.

ThegeniecananswerYES/NO

questionsoftheform:

is [this] bigger than [that]?Algorithm

Wanttosorttheseitems.

There’ssomeorderingonthem,butwedon’tknowwhatitis.

Is bigger than ?

YES

Thealgorithm’sjobisto

outputacorrectlysorted

listofalltheobjects.

isshorthandfor

“thefirstthingintheinputlist”

Page 9: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Allthesortingalgorithmswehaveseenworklikethis.

7 6 3 5 1 4 2eg,QuickSort:

Is bigger than ? 7 5

Is bigger than ?

Is bigger than ?

6

3

5

5

YES

YES

NO

7 6 3

5 etc.

Pivot!

Page 10: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

LowerboundofΩ(nlog(n)).

• Theorem:

• Anydeterministiccomparison-basedsortingalgorithm musttakeΩ(nlog(n))steps.

• Anyrandomized comparison-basedsortingalgorithm musttakeΩ(nlog(n))stepsinexpectation.

• Howmightweprovethis?

1. Considerallcomparison-basedalgorithms,one-by-one,andanalyzethem.

2. Don’tdothat.

Instead,arguethatallcomparison-basedsorting

algorithmsgiverisetoadecisiontree.

Thenanalyzedecisiontrees.

Page 11: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Decisiontrees

Sortthesethreethings. ?≤

YESNO

YES

?

NO

≤ ?YES NO

etc…

Page 12: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Allcomparison-basedalgorithmslooklikethis

Example:Sortthese

threethingsusing

QuickSort.

NO

?

YES

L RRL

≤ ?NOYES

L RL RReturn ≤

NOYES

?Thenwe’redone

(aftersomebase-

casestuff)

Now

recurse

onR

Pivot!

L RL R

Pivot!

Return ReturnIneithercase,we’redone

(aftersomebasecasestuffand

returningrecursivecalls).

etc...

Page 13: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Allcomparison-basedalgorithmshaveanassociateddecisiontree.

YES NO

?

??

YESNOYES

NO

????

Theleavesofthis

treeareallpossible

orderingsofthe

items:whenwe

reachaleafwe

returnit.

Whatdoesthedecision

treeforMERGESORTING

fourelementslooklike?

Olliethe

over-achievingostrich

Runningthealgorithmonagiven

inputcorrespondstotakinga

particularpaththroughthetree.

Page 14: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

What’stheruntimeonaparticularinput?

YES NO

?

??

YESNOYES

NO

????

Ifwetakethispaththrough

thetree,theruntimeis

Ω(lengthofthepath).

Atleastthenumber

ofcomparisonsthat

aremadeonthat

input.

Page 15: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

What’stheworst-case runtime?

YES NO

?

??

YESNOYES

NO

????

AtleastΩ(lengthofthelongestpath).

Page 16: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Howlongisthelongestpath?

YESNO

?

??YES

NOYESNO

????

• Thisisabinarytreewithat

least_____leaves.

• Theshallowesttreewithn!

leavesisthecompletely

balancedone,whichhas

depth______.

• Soinallsuchtrees,the

longestpathisatleastlog(n!).

n!

log(n!)

• n!isabout(n/e)n (Stirling’s approx.*).

• log(n!)isaboutnlog(n/e)=Ω(nlog(n)).Conclusion:thelongestpath

haslengthatleastΩ(nlog(n)).

beingsloppyabout

floorsandceilings!

Wewantastatement:inallsuchtrees,

thelongestpathisatleast_____

*Stirling’s approximationisabitmorecomplicatedthanthis,butthisisgoodenoughfortheasymptoticresultwewant.

Page 17: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

LowerboundofΩ(nlog(n)).• Theorem:• Anydeterministiccomparison-basedsortingalgorithm musttakeΩ(nlog(n)) steps.

• Proofrecap:• Anydeterministiccomparison-basedalgorithmcanberepresentedasadecisiontreewithn!leaves.

• Theworst-caserunningtimeisatleastthedepthofthedecisiontree.

• Alldecisiontreeswithn!leaveshavedepthΩ(nlog(n)).

• Soanycomparison-basedsortingalgorithmmusthaveworst-caserunningtimeatleast Ω(nlog(n)).

Page 18: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

\end{Aside}

• Forexample,QuickSort?

• Theorem:

• Anyrandomized comparison-basedsortingalgorithmmusttakeΩ(nlog(n))stepsinexpectation.

• Proof:

• seelecturenotes

• (sameideasasdeterministiccase) Trytoprovethis

yourself!

Ollietheover-achievingostrich

Aside:Whataboutrandomizedalgorithms?

Page 19: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Butlookonthebrightside!

• Theorem:

• Anydeterministiccomparison-basedsortingalgorithm musttakeΩ(nlog(n)) steps.

• Theorem:

• Anyrandomized comparison-basedsortingalgorithm musttakeΩ(nlog(n))stepsinexpectation.

Sothat’sbadnews.

Page 20: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

ButwhataboutStickSort?

• Thisisoneofthecoolthingsaboutlowerboundslikethis:weknowwhenwecandeclarevictory!

MergeSort isoptimal!

• StickSort can’tbeimplementedasacomparison-basedsortingalgorithm.Sotheselowerboundsdon’tapply.

• ButStickSort waskindofdumb.EspeciallyifIhave

tospendtime

cuttingallthose

stickstobethe

rightsize!Butmighttherebeanothermodelofcomputationthat’slessdumb,inwhichwecansortfaster?

Page 21: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Beyondcomparison-basedsortingalgorithms

Page 22: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Anothermodelofcomputation

• Theitemsyouaresortinghavemeaningfulvalues.

9 6 3 5 2 1 2

insteadof

Page 23: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Pre-lectureexercise

• SortingCS161studentsbytheirmonthofbirth.

• [Discussiononboard]

1 1 4 5

Page 24: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Anothermodelofcomputation

• Theitemsyouaresortinghavemeaningfulvalues.

9 6 3 5 2 1 2

insteadof

Page 25: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Whymightthishelp?

BucketSort: 9 6 3 5 2 1 2

1 2 3 4 5 6 7 8 9

963 521

2

SORTED!IntimeO(n).

Implementthebuckets aslinked

lists.Theyarefirst-in,first-out.

Thiswillbeusefullater.

Concatenate

thebuckets!

Note:thisisasimplificationof

whatCLRScalls“BucketSort”

Page 26: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Issues

• Needtobeabletoknowwhatbuckettoputsomethingin.

• Wheredoesgo?

• That’sokayfornow:it’spartofthemodel.

• Needtoknowwhatvaluesmightshowupaheadoftime.

• Space…

2 12345 13 21000 50 100000000 1

Page 27: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Onesolution:RadixSortSaywe’resortingintegers.

• Idea:BucketSort ontheleast-significantdigitfirst,thenthenextleast-significant,andsoon.

1 2 3 4 5 6 7 8 9

21 345 13 101 50 234 1

0

345

50 1321

101

1

234

Step1:BucketSort onLSB:

50 21 101 1 13 234 345

Page 28: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Step2:BucketSort onthe2nd digit

1 2 3 4 5 6 7 8 90

50 21 101 1 13 234 345

502113101

234

1 345

101 1 13 21 234 345 50

Page 29: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Step3:BucketSort onthe3rd digit

1 2 3 4 5 6 7 8 90

50

21

13

101

234

1

345

1 13 21 50 101 234 345

101 1 13 21 234 345 50

Itworked!!

Page 30: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Whydoesthiswork?

21 345 13 101 50 234 1

50 21 101 1 13 234 345

1 13 21 50 101 234 345

101 1 13 21 234 345 50

Originalarray:

Nextarrayissortedbythefirstdigit.

Nextarrayissortedbythefirsttwodigits.

Nextarrayissortedbyallthreedigits.

Sortedarray

50 21 101 1 13 234 345

101 01 13 21 234 345 50

001 013 021 050 101 234 345

Page 31: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Formally…

• Arguebyinduction.

• Inductivehypothesis:

Luckythelackadaisicallemur

Oratleasta

littleformally!

Page 32: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Whydoesthiswork?

21 345 13 101 50 234 1

50 21 101 1 13 234 345

1 13 21 50 101 234 345

101 1 13 21 234 345 50

Originalarray:

Nextarrayissortedbythefirstdigit.

Nextarrayissortedbythefirsttwodigits.

Nextarrayissortedbyallthreedigits.

Sortedarray

50 21 101 1 13 234 345

101 01 13 21 234 345 50

001 013 021 050 101 234 345

Page 33: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Formally…

• Arguebyinduction.

• Inductivehypthesis:

• Afterthek’th iteration,thearrayissortedbythefirstkleast-significantdigits.

• Basecase:

• “Sortedby0least-significantdigits”meansnotsorted.

• Inductivestep:

• (SeelecturenotesorCLRS)

• Conclusion:

• Afterthed’th iteration,thearrayissortedbythedleast-significantdigits.Aka,it’ssorted.

Luckythelackadaisicallemur

Oratleasta

littleformally!

Pluckythepedanticpenguin

Thisneedstouse:(1)bucketsort

works,and(2)wetreateachbucket

asaFIFOqueue.*

*thebuzzwordhereisthat

bucketSort isstable.

Page 34: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Canwedobetter?whatifM=n?

• Saytheyared-digit numbers.

• Therearediterations.

• EachiterationtakestimeO(n+10)=O(n)

• Totaltime:O(nd).

• SaythebiggestintegerisM.Whatisd?

• d= log%& 𝑀 + 1

• soO(nd)=O(nlog10(M)).

Whatistherunningtime?The“10”isbecausewe

areworkingbase10.

Page 35: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Trade-offs…

• RadixSort workswithanybase.

• Beforewediditbaser=10.

• Butwecoulddoitbaser=2 orr=20 justaseasily.

• [Onboard]

• RunningtimeforgeneralrandM?

• [Onboard]

Page 36: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Trade-offsctd…• Therearennumbers,biggestoneisM.

• Whatshouldwechooseforr(intermsofM,n)?

Runningtime:𝑂 (𝑛 + 𝑟) ⋅ log0 𝑀

There’ssomesweetspot… (andmaybeit’sgrowingwithMandn?)

IPython NotebookforLecture6

Page 37: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Weget…

• [Discussiononboard…]

• Ifwechooser=n,runningtimeis𝑻 𝒏 = 𝑶 𝒏 ⋅ 𝐥𝐨𝐠𝐧 𝑴

• IfM=O(n),T(n)=O(n).Awesome!

• IfM=Ω(nn),T(n)=O(n2)…

Ollietheover-achievingostrich

Choosingr=n

isprettygood.

What’stheoptimal

choiceofr?

Page 38: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Thestorysofar

• Ifweuseacomparison-basedsortingalgorithm,itMUSTrunintimeΩ(nlog(n)).

• Ifweassumeabitofstructureonthevalues,wehaveanO(n)-timesortingalgorithm.

9 6 3 5 2 1 2

Whywouldweeverusea

comparison-basedsortingalgorithm??

Page 39: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Whywouldweeveruse

acomparison-basedsortingalgorithm?

• Lotsofprecision…

• RadixSort needsextramemoryforthebuckets.• Notin-place

• Iwanttosortemojibytalkingtoagenie.• RadixSort makesmoreassumptionsontheinput.

𝜋123456

987654 𝑒 140! 2.1234123 nn 42

• Wecancomparetheseprettyquickly(justlookatthemost-significantdigit):

• 𝜋 =3.14….

• e=2.78….

• ButtodoRadixSort we’dhavetolookateverydigit.

• Thisisespeciallyproblematicsincebothofthesehaveinfinitelymanydigits...

Evenwithintegers,ifthe

biggestonisreallybig,

RadixSort isslow.

Page 40: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Recap• Howdifficultaproblemisdependsonthemodelofcomputation.

• Howreasonableamodelofcomputationisisupfordebate.

• Comparison-basedsortingmodel

• ThisincludesMergeSort,QuickSort,InsertionSort

• Any algorithminthismodelmustuseatleastΩ(nlog(n))operations.

• Butifwearesortingsmallintegers(orotherreasonabledata):

• BucketSort andRadixSort

• BothrunintimeO(n)

Page 41: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted

Nexttime

• Binarysearchtrees!

• Balancedbinarysearchtrees!

• Specialguestlecturer:SamKim!

• Pre-lectureexerciseforLecture7

• Rememberbinarysearchtrees?

Before nexttime

Page 42: Lecture 6 - Stanford Universityweb.stanford.edu/.../Lecture6/Lecture6-compressed.pdfLecture 6 Sorting lower bounds and O(n)-time sorting Announcements •HW2 due Friday •HW3 posted