sorting- algorithms -...
TRANSCRIPT
![Page 1: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/1.jpg)
Sortingrace
https://www.toptal.com/developers/sorting-algorithms
![Page 2: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/2.jpg)
CS2230CSII:Datastructures
ComparisonsortingBrandonMyers
UniversityofIowa
![Page 3: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/3.jpg)
Today’sLearningObjectives
• Executeseveralcomparisonsortingalgorithms• Analyzetherunningtimeseveralcomparisonsortingalgorithm
![Page 4: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/4.jpg)
Definitionofasort
Theoperator≼ definesatotalorderoveracollectionofitems.Asort w.r.t ≼isapermutationoftheitemssuchthattheorderobeys≼.
Forexample,Ifx$ ≼ 𝑥& wherex$ isastringthatcomesbeforex&inthedictionarythen
”cat”≼ “catnip”≼ “dog”
![Page 5: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/5.jpg)
Wakeupyourbrain
Write:Youhaveacollectionof100catswithnametags;howwouldyousortthemalphabeticallybyname?
![Page 6: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/6.jpg)
Wakeupyourbrain
üWrite:Youhaveacollectionof100catswithnametags;howwouldyousortthemalphabeticallybyname?
Clicker:runningtimeforusingyouralgorithmonNcats?
https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+
![Page 7: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/7.jpg)
Selectionsort
fori =0toN-1findthesmallestitemin[i,N-1]swapitwithitemi
seedemohttp://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html
![Page 8: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/8.jpg)
Executeavarietyofcomparisonsortingalgorithms
We’resomewhereinthemiddleofrunningselectionsortonthisarray.Whattwoitemswillbeswappednext?
https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+
5692715242229141613
![Page 9: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/9.jpg)
Selectionsortfori =0toN-1findthesmallestitemin[i,N-1]swapitwithitemi
RunningtimetoselectionsortNelementsinanarray?
a) Θ(N)b) Θ(NlogN)c) Θ(N2)d) Θ(N3)e) Θ(2N)
Analyzetherunningtimeseveralcomparisonsortingalgorithm
https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+
![Page 10: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/10.jpg)
forfun
https://www.youtube.com/watch?v=kPRA0W1kECg
![Page 11: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/11.jpg)
http://sortbenchmark.org/GraySort
Metric: Sort rate (TBs / minute) achieved while sorting a very large amount of data (currently 100 TB minimum).
2016,100TBin134Seconds
CloudSortMetric: Minimum cost for sorting a very large amount of data on a public cloud. (currently 100 TB).
2016,100TBfor$144
MinuteSortMetric: Amount of data that can be sorted in 60.00 seconds or less.
2016,37TB
JouleSortMetric: Amount of energy required to sort either 10
8, 10
9, 10
10, or 10
12records (10 GB, 100
GB, 1 TB, or 100TB).
2016,168,242Joules for1010
PennySortMetric: Amount of data that can be sorted for a penny's worth of system time.
2011, 286 GB
solutionstothiscompetitionconsiderbothhardwareandsoftwaretogether
moreserious
![Page 12: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/12.jpg)
Tencent Sort;Jie Jiang*,Lixiong Zheng*,Junfeng Pu*,Xiong Cheng*,ChongqingZhao*,MarkRNutter**,JeremyDSchaub***Tencent Corporation,China**TechnicalSupport
512serversinparallel
radixsorteachsegment,1percore(20cores)
sendeachrecordtoaserverbasedonitsrange mergesortto
finish
dataintosegments
![Page 13: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/13.jpg)
Heapsort
1.Foreachitem,insertitintoaminheap.
2.NowcalldeleteMin,addingtheitemtothearray,untilnoitemsareleftintheheap.
(usetheheapdemotosimulateheapsort)http://www.cs.usfca.edu/~galles/visualization/Heap.html
6275152422291416139
![Page 14: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/14.jpg)
Heapsort1.Foreachitem,insertitintoamaxheap.
2.NowcalldeleteMax,puttingtheelementinthebackofthearray,untilnoelementsareleft.
RunningtimetoheapsortNelementsinanarray?
a) Θ(logN)b) Θ(N)c) Θ(NlogN)d) Θ(N2)e) Θ(N3)
Analyzetherunningtimeseveralcomparisonsortingalgorithm
https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+
![Page 15: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/15.jpg)
Heapsortbest/worstcase
WhynobetterthanNlogN?
• agoodcase? “theinputisalreadysorted”• Aswebuildtheheap,nobubbleupwillbenecessary,soO(1)perelement,soO(N)totalforbuild(sofarsogood:betterthanO(NlogN)intheworstcase)• WhenwedeleteMin,weareforcedtoswaptherootwiththelastelementandbubbleitbackdown,soO(logS)perelement(whereSiscurrentsize).SummationoverNelementsgivesO(NlogN)
![Page 16: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/16.jpg)
MemoryusageofheapsortRequiresO(N)additionalmemorytobuildtheheap(anextracopyofthedata)
touseO(1)additionalmemoryinstead,youcandoheapsortin-place• interprettheoriginalarrayasabinarytree• turnitintoavalidmaxbinaryheap• whenremovingelements,utilizetheendofthe
array
demohttp://www.cs.usfca.edu/~galles/visualization/HeapSort.html
![Page 17: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/17.jpg)
Insertionsort
startwithanemptyoutputlinkedlist
fori =0toN-1item=input[i]insertitemintothelinkedlistinsortedorder
![Page 18: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/18.jpg)
Insertionsort,in-place
fori =0toN-1item=input[i]swapitemwithnextelementuntilinsortedorder
http://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html
![Page 19: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/19.jpg)
Executeavarietyofcomparisonsortingalgorithms
Runin-placeinsertionsortonthisarray.
Clicker:Howmanyswaps totalwererequired?
https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+
17233011012226288721
![Page 20: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/20.jpg)
Insertionsort,inplacefori =0toN-1
item=input[i]swapitemwithnextelementuntilinsortedorder
RunningtimetoinsertionsortNelementsinanarray?
Bestcase,worstcasea) O 1 ,O(N)b) O 1 ,O(N2)c) O 𝑁 ,O(N)d) O 𝑁 ,O(N2)e) O 𝑁2 ,O(N2)
Analyzetherunningtimeseveralcomparisonsortingalgorithm
https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+
![Page 21: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/21.jpg)
Mergesort
breakarrayintotwohalves,recursivelymergesorteachone
basecaseistwoitems
merge theleftandrighthalf
![Page 22: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/22.jpg)
Mergesortin-place
http://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html
![Page 23: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/23.jpg)
Executeseveralcomparisonsortingalgorithms
Runin-placemergesortonthisarray.Writeanewlineeachtimeelementschangeposition.
12226288721192511183420
![Page 24: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/24.jpg)
Mergesort
Runningtimetomergesort Nelementsinanarray?
a) Θ(logN)b) Θ(N)c) Θ(NlogN)d) Θ(N2)e) Θ(N3)
Analyzetherunningtimeseveralcomparisonsortingalgorithm
https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+
![Page 25: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/25.jpg)
quicksort
1. picka“pivot”2. foreachitemlessthan“pivot”putitintheleft
sideofthearray3. foreachitemgreaterthanorequalto“pivot”put
itintherightsideofthearray4. callquicksortonleft,callquicksortonright
![Page 26: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/26.jpg)
quicksortvisualization(usesin-placequicksort)
http://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html
![Page 27: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/27.jpg)
50 80 38 12 99 44
Wecalledquicksortontheabovearray.Let’ssupposewealways pickthesecondelementofthearrayasthepivot (i.e.80above).Whatwillbethenexttworecursivecallstoquicksort(assumingwecallitonleftthenright)?
50 38 12 44
12
50 38 12 44
80 99
50 38 12 44
99
12 38 44 50
12 44
12 38 44 50
80 99
a)
b)
c)
d)
e)
https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+
![Page 28: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/28.jpg)
Whatvaluewouldmakethebestpivot?
a) Arandomvalueb) Theminimumoftheitemsc) Themeanoftheitemsd) Thethevalueofthefirstiteme) Thethemedianoftheitems
Peerinstruction
https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+
![Page 29: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/29.jpg)
Runningtimeofquicksort
assumingtheworstpivoteverytime?
assumingthebestpivoteverytime?
![Page 30: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/30.jpg)
Pickingagoodpivotcheaply?
manyproposals
Thesethreeareokayandtakecareofthesorted/almostsortedcases• medianofthree• randomindex• middleindexofthepartition• ...
Whatiftherearemanyduplicatesofavalue(thisiscommon!WhatifyouaresortingRetweetsbyoriginalposter?)• createathirdbucketforthecommonvalue;don’tneedtosortitsinceitisalreadysorted
![Page 31: sorting- algorithms - homepage.cs.uiowa.eduhomepage.cs.uiowa.edu/.../public/lectures/lecture-037-sorting.pdf · •Execute several comparison sorting algorithms •Analyze the running](https://reader030.vdocuments.net/reader030/viewer/2022020105/5cb0111f88c993767e8c3fb0/html5/thumbnails/31.jpg)
Today’sLearningObjectives
• Executeseveralcomparisonsortingalgorithms• Analyzetherunningtimeseveralcomparisonsortingalgorithm