02-Analysis of Insertion Sort - cs.albany.edupetko/classes/FALL16-CSI403...Insertion sort growth...

12
403: Algorithms and Data Structures Analysis of Insertion Sort Fall 2016 UAlbany Computer Science

Transcript of 02-Analysis of Insertion Sort - cs.albany.edupetko/classes/FALL16-CSI403...Insertion sort growth...

Page 1: 02-Analysis of Insertion Sort - cs.albany.edupetko/classes/FALL16-CSI403...Insertion sort growth rate • Consider insertion sort’s running time as the function d 1n2 + d 2n + d

403:AlgorithmsandDataStructures

AnalysisofInsertionSortFall2016UAlbany

ComputerScience

Page 2: 02-Analysis of Insertion Sort - cs.albany.edupetko/classes/FALL16-CSI403...Insertion sort growth rate • Consider insertion sort’s running time as the function d 1n2 + d 2n + d

Tune-inexercise

• Whatisalgorithmanalysis?–Meanstopredicttheresourcesneededforanalgorithmexecution.

• Whatresourcesareweconcernedwith?– Runningtimeandmemory

• Whydoweneedsuchresourceprediction?– Tobeabletocomparealgorithms– Tobeabletoprovisionresourcesforalgorithmexecution

Page 3: 02-Analysis of Insertion Sort - cs.albany.edupetko/classes/FALL16-CSI403...Insertion sort growth rate • Consider insertion sort’s running time as the function d 1n2 + d 2n + d

Exampleofinsertionsortonaninstance

• Whichisthealgorithm?• Whichistheinput?• Whichistheoutput?• Whatistheinstance?

Thealgorithm

Theinput

Theoutput

<5,2,4,6,1,3>

Page 4: 02-Analysis of Insertion Sort - cs.albany.edupetko/classes/FALL16-CSI403...Insertion sort growth rate • Consider insertion sort’s running time as the function d 1n2 + d 2n + d

Exampleofinsertionsortonaninstance

Takeaminutetothinkonyourownofwhatishappeningateachstep.

Page 5: 02-Analysis of Insertion Sort - cs.albany.edupetko/classes/FALL16-CSI403...Insertion sort growth rate • Consider insertion sort’s running time as the function d 1n2 + d 2n + d

Insertionsort(pseudocode)

Thisstepcanbereachedwheni=0orifA[i]≤key.Inbothcaseskeyisplaceds.t. A[1…i]issorted

Inputarrayis1-based

j indexesthewhole

array

iindexesthesortedsequence

Page 6: 02-Analysis of Insertion Sort - cs.albany.edupetko/classes/FALL16-CSI403...Insertion sort growth rate • Consider insertion sort’s running time as the function d 1n2 + d 2n + d

Insertionsort-- analysis

• Recallthateachprimitiveoperationtakesconstanttime

• Assumetherearen numbersintheinput

• c1,c2,andc3 areconstantsanddonotdependonn

c1

c2c3

Page 7: 02-Analysis of Insertion Sort - cs.albany.edupetko/classes/FALL16-CSI403...Insertion sort growth rate • Consider insertion sort’s running time as the function d 1n2 + d 2n + d

Insertionsort-- analysis

• Assumetherearen numbersintheinput

c2

c1

c3

Whatisthetimeneededforthealgorithmexecution?

Page 8: 02-Analysis of Insertion Sort - cs.albany.edupetko/classes/FALL16-CSI403...Insertion sort growth rate • Consider insertion sort’s running time as the function d 1n2 + d 2n + d

Insertionsort-- analysis• Assumetherearen numbersintheinput

• While loopisexecutedatmostj-1 timesforagiven j,sotimespentinloopisatmost(j-1)c2

• AnyiterationoftheouterFor looptakesatmostc1 +(j-1)c2+c3

• Theoverallrunningtimeofinsertionsortis∑ c1+(j-1)c2+c3𝒏𝒋$𝟐 =d1n2 +d2n+d3

c2

c1

c3

Page 9: 02-Analysis of Insertion Sort - cs.albany.edupetko/classes/FALL16-CSI403...Insertion sort growth rate • Consider insertion sort’s running time as the function d 1n2 + d 2n + d

Wasouranalysistoopessimistic?

• Wejustperformedaworst-caseanalysisofinsertionsort,whichgaveusanupperboundoftherunningtime.

• Wasouranalysistoopessimistic?Inotherwords,arethereinstancesthatwillcausethealgorithmtorunwithquadratictimeinn?– Theworst-caseinstanceisareverse-sortedsequencea1,a2,…,an suchthata1>a2>…>an

• Sinceworst-casesequenceexists,wesaythatouranalysisis“tight”and”notpessimistic”.

Page 10: 02-Analysis of Insertion Sort - cs.albany.edupetko/classes/FALL16-CSI403...Insertion sort growth rate • Consider insertion sort’s running time as the function d 1n2 + d 2n + d

Insertionsortgrowthrate

• Considerinsertionsort’srunningtimeasthefunctiond1n2 +d2n+d3– Thedominantpartofthisfunctionisn2 (i.e.asnbecomeslarge,n2 growsmuchfasterthann)

– Thus,thegrowthrateoftherunningtimeisdeterminedbythen2 term

–WeexpressthisasO(n2)(a.k.a.“big-oh”notation*)–Wecomparealgorithmsintermsoftheirrunningtime

*Tobeformallydefinedlater

Page 11: 02-Analysis of Insertion Sort - cs.albany.edupetko/classes/FALL16-CSI403...Insertion sort growth rate • Consider insertion sort’s running time as the function d 1n2 + d 2n + d

Algorithmcomparison• Whichalgorithmisbetter?

– Weanswerthisquestionbycomparingalgorithms’O()runningtimes.

• Example:ComparealgorithmAandB.Whichoneisbetter?– AlgorithmA:O(n2)– AlgorithmB:O(nlog2(n))

ByCmglee - Ownwork,CCBY-SA4.0,https://commons.wikimedia.org/w/index.php?curid=50321072

• Bismoreefficient.• Intuitivelyn2 growsfaster• Wemightbewrongforsmallinstancesbutwhen nislargeBwillbefaster

• Largesizescomeaboutveryoften(Facebookhas100sofmillionsofusers)

Page 12: 02-Analysis of Insertion Sort - cs.albany.edupetko/classes/FALL16-CSI403...Insertion sort growth rate • Consider insertion sort’s running time as the function d 1n2 + d 2n + d

Announcements

• ReadthroughChapters1and2inthebook• Homework1posted,DueonSep7