![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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/1.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/15.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/16.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/17.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/18.jpg)
\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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/19.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/20.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/21.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/22.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/23.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/24.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/25.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/26.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/27.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/28.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/29.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/30.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/31.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/32.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/33.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/34.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/35.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/36.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/37.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/38.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/39.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/40.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/41.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070906/5f76a32c9471ca1fbf133d30/html5/thumbnails/42.jpg)