sorting- algorithms -...

31
Sorting race https://www.toptal.com/developers/sorting- algorithms

Upload: ngoliem

Post on 12-Apr-2019

253 views

Category:

Documents


0 download

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Today’sLearningObjectives

• Executeseveralcomparisonsortingalgorithms• Analyzetherunningtimeseveralcomparisonsortingalgorithm