1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method...
-
date post
18-Dec-2015 -
Category
Documents
-
view
214 -
download
1
Transcript of 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method...
11
Chapter 4Chapter 4Analysis ToolsAnalysis Tools
22
Which is faster – selection sort or insertion sort?
Potential method for evaluation:Implement each as a method and then Time each method to see which is faster
33
What are the most important criteria that influence our algorithm implementation choices?
What do each of these criteria directly affect?
44
Experimental StudiesExperimental Studies
Write a program Write a program implementing the algorithmimplementing the algorithmRun the program with Run the program with inputs of varying size and inputs of varying size and compositioncompositionUse a method like Use a method like System.currentTimeMillis()System.currentTimeMillis() to to get an accurate measure get an accurate measure of the actual running timeof the actual running timePlot the resultsPlot the results
0
1000
2000
3000
4000
5000
6000
7000
8000
9000
0 50 100
Input Size
Tim
e (m
s)
55
Limitations of ExperimentsLimitations of Experiments
It is necessary to implement the It is necessary to implement the algorithm, which may be difficultalgorithm, which may be difficultResults may not be indicative of the Results may not be indicative of the running time on other inputs not included running time on other inputs not included in the experiment. in the experiment. In order to compare two algorithms, the In order to compare two algorithms, the same hardware and software same hardware and software environments must be usedenvironments must be used
66
Theoretical AnalysisTheoretical Analysis
Uses a high-level description of the Uses a high-level description of the algorithm instead of an implementationalgorithm instead of an implementation
Characterizes running time as a Characterizes running time as a function of the input size, function of the input size, nn..
Takes into account all possible inputsTakes into account all possible inputs
Allows us to evaluate the speed of an Allows us to evaluate the speed of an algorithm independent of the algorithm independent of the hardware/software environmenthardware/software environment
77
Big-O
0
0
allfor
such that , and constants, positive two
exist thereiff is function A
nn
ngcnf
nc
ngOnf
Two important rules:Make g(n) as small as possibleg(n) never contains unnecessary terms
Asymptotic AnalysisThe goal of asymptotic analysis is to determine the complexity order of an algorithm
88
Big-Oh RulesBig-Oh Rules
If is If is ff((nn)) a polynomial of degree a polynomial of degree dd, then , then ff((nn)) is is OO((nndd)), i.e.,, i.e.,
1.1. Drop lower-order termsDrop lower-order terms
2.2. Drop constant factorsDrop constant factors
Use the smallest possible class of functionsUse the smallest possible class of functions Say “Say “22nn is is OO((nn))”” instead of “instead of “22nn is is OO((nn22))””
Use the simplest expression of the classUse the simplest expression of the class Say “Say “33nn 55 is is OO((nn))”” instead of “instead of “33nn 55 is is OO(3(3nn))””
99
1010
Figure 9.6Figure 9.6An insertion sort partitions the array into two regions
1111
Figure 9.7Figure 9.7An insertion sort of an array of five integers.
1212
1313
Figure 9.4Figure 9.4A selection sort of an array of five integers
1414
Order of Complexity•Exponential•Polynomial•Log•Linear•Constant
Exponential > Polynomial > Log > Linear > Constant
1515
Figure 9.3aFigure 9.3aA comparison of growth-rate functions: a) in tabular form
1616
Figure 9.3bFigure 9.3bA comparison of growth-rate functions: b) in graphical form
1717
1818
1919
Worst case: largest value for any problem of size n
Best case: smallest value for any problem of size n
Average case: (weighted) average of all problems of size n
2020
Figure 9.5Figure 9.5The first two passes of a bubble sort of an array of five integers: a) pass 1; b) pass 2
2121
Figure 9.8Figure 9.8A mergesort with an auxiliary temporary array
2222
Figure 9.9Figure 9.9A mergesort of an array of six integers
2323
Figure 9.10Figure 9.10A worst-case instance of the merge step in mergesort
2424
Figure 9.11Figure 9.11Levels of recursive calls to mergesort given an array of eight items
2525
Figure 9.12Figure 9.12A partition about a pivot
2626
Figure 9.13Figure 9.13kSmall versus quicksort
2727
Figure 9.14Figure 9.14Invariant for the partition algorithm
2828
Figure 9.15Figure 9.15Initial state of the array
2929
Figure 9.16Figure 9.16Moving theArray[firstUnknown] into S1 by swapping it with theArray[lastS1+1] and by incrementing both lastS1 and firstUnknown
3030
Figure 9.17Figure 9.17Moving theArray[firstUnknown] into S2 by incrementing firstUnknown
3131
Figure 9.18aFigure 9.18aDeveloping the first partition of an array when the pivot is the first item
3232
Figure 9.18bFigure 9.18bDeveloping the first partition of an array when the pivot is the first item
3333
Figure 9.19Figure 9.19A worst-case partitioning with quicksort
3434
Figure 9.20Figure 9.20A average-case partitioning with quicksort
3535
Figure 9.21Figure 9.21A radix sort of eight integers
3636
Figure 9.22Figure 9.22Approximate growth rates of time required for eight sorting algorithms