sorting- algorithms -...
TRANSCRIPT
Sortingrace
https://www.toptal.com/developers/sorting-algorithms
CS2230CSII:Datastructures
ComparisonsortingBrandonMyers
UniversityofIowa
Today’sLearningObjectives
• Executeseveralcomparisonsortingalgorithms• Analyzetherunningtimeseveralcomparisonsortingalgorithm
Definitionofasort
Theoperator≼ definesatotalorderoveracollectionofitems.Asort w.r.t ≼isapermutationoftheitemssuchthattheorderobeys≼.
Forexample,Ifx$ ≼ 𝑥& wherex$ isastringthatcomesbeforex&inthedictionarythen
”cat”≼ “catnip”≼ “dog”
Wakeupyourbrain
Write:Youhaveacollectionof100catswithnametags;howwouldyousortthemalphabeticallybyname?
Wakeupyourbrain
üWrite:Youhaveacollectionof100catswithnametags;howwouldyousortthemalphabeticallybyname?
Clicker:runningtimeforusingyouralgorithmonNcats?
https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+
Selectionsort
fori =0toN-1findthesmallestitemin[i,N-1]swapitwithitemi
seedemohttp://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html
Executeavarietyofcomparisonsortingalgorithms
We’resomewhereinthemiddleofrunningselectionsortonthisarray.Whattwoitemswillbeswappednext?
https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+
5692715242229141613
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+
forfun
https://www.youtube.com/watch?v=kPRA0W1kECg
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
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
Heapsort
1.Foreachitem,insertitintoaminheap.
2.NowcalldeleteMin,addingtheitemtothearray,untilnoitemsareleftintheheap.
(usetheheapdemotosimulateheapsort)http://www.cs.usfca.edu/~galles/visualization/Heap.html
6275152422291416139
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+
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)
MemoryusageofheapsortRequiresO(N)additionalmemorytobuildtheheap(anextracopyofthedata)
touseO(1)additionalmemoryinstead,youcandoheapsortin-place• interprettheoriginalarrayasabinarytree• turnitintoavalidmaxbinaryheap• whenremovingelements,utilizetheendofthe
array
demohttp://www.cs.usfca.edu/~galles/visualization/HeapSort.html
Insertionsort
startwithanemptyoutputlinkedlist
fori =0toN-1item=input[i]insertitemintothelinkedlistinsortedorder
Insertionsort,in-place
fori =0toN-1item=input[i]swapitemwithnextelementuntilinsortedorder
http://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html
Executeavarietyofcomparisonsortingalgorithms
Runin-placeinsertionsortonthisarray.
Clicker:Howmanyswaps totalwererequired?
https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+
17233011012226288721
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+
Mergesort
breakarrayintotwohalves,recursivelymergesorteachone
basecaseistwoitems
merge theleftandrighthalf
Mergesortin-place
http://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html
Executeseveralcomparisonsortingalgorithms
Runin-placemergesortonthisarray.Writeanewlineeachtimeelementschangeposition.
12226288721192511183420
Mergesort
Runningtimetomergesort Nelementsinanarray?
a) Θ(logN)b) Θ(N)c) Θ(NlogN)d) Θ(N2)e) Θ(N3)
Analyzetherunningtimeseveralcomparisonsortingalgorithm
https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+
quicksort
1. picka“pivot”2. foreachitemlessthan“pivot”putitintheleft
sideofthearray3. foreachitemgreaterthanorequalto“pivot”put
itintherightsideofthearray4. callquicksortonleft,callquicksortonright
quicksortvisualization(usesin-placequicksort)
http://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html
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+
Whatvaluewouldmakethebestpivot?
a) Arandomvalueb) Theminimumoftheitemsc) Themeanoftheitemsd) Thethevalueofthefirstiteme) Thethemedianoftheitems
Peerinstruction
https://b.socrative.com/login/student/roomCS2230Xids1000-2999roomCS2230Yids3000+
Runningtimeofquicksort
assumingtheworstpivoteverytime?
assumingthebestpivoteverytime?
Pickingagoodpivotcheaply?
manyproposals
Thesethreeareokayandtakecareofthesorted/almostsortedcases• medianofthree• randomindex• middleindexofthepartition• ...
Whatiftherearemanyduplicatesofavalue(thisiscommon!WhatifyouaresortingRetweetsbyoriginalposter?)• createathirdbucketforthecommonvalue;don’tneedtosortitsinceitisalreadysorted
Today’sLearningObjectives
• Executeseveralcomparisonsortingalgorithms• Analyzetherunningtimeseveralcomparisonsortingalgorithm