1 chapter 4 analysis tools. 2 which is faster – selection sort or insertion sort? potential method...
Post on 18-Dec-2015
214 views
TRANSCRIPT
![Page 1: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/1.jpg)
11
Chapter 4Chapter 4Analysis ToolsAnalysis Tools
![Page 2: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/2.jpg)
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
![Page 3: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/3.jpg)
33
What are the most important criteria that influence our algorithm implementation choices?
What do each of these criteria directly affect?
![Page 4: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/4.jpg)
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)
![Page 5: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/5.jpg)
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
![Page 6: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/6.jpg)
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
![Page 7: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/7.jpg)
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
![Page 8: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/8.jpg)
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))””
![Page 9: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/9.jpg)
99
![Page 10: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/10.jpg)
1010
Figure 9.6Figure 9.6An insertion sort partitions the array into two regions
![Page 11: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/11.jpg)
1111
Figure 9.7Figure 9.7An insertion sort of an array of five integers.
![Page 12: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/12.jpg)
1212
![Page 13: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/13.jpg)
1313
Figure 9.4Figure 9.4A selection sort of an array of five integers
![Page 14: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/14.jpg)
1414
Order of Complexity•Exponential•Polynomial•Log•Linear•Constant
Exponential > Polynomial > Log > Linear > Constant
![Page 15: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/15.jpg)
1515
Figure 9.3aFigure 9.3aA comparison of growth-rate functions: a) in tabular form
![Page 16: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/16.jpg)
1616
Figure 9.3bFigure 9.3bA comparison of growth-rate functions: b) in graphical form
![Page 17: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/17.jpg)
1717
![Page 18: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/18.jpg)
1818
![Page 19: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/19.jpg)
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
![Page 20: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/20.jpg)
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
![Page 21: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/21.jpg)
2121
Figure 9.8Figure 9.8A mergesort with an auxiliary temporary array
![Page 22: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/22.jpg)
2222
Figure 9.9Figure 9.9A mergesort of an array of six integers
![Page 23: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/23.jpg)
2323
Figure 9.10Figure 9.10A worst-case instance of the merge step in mergesort
![Page 24: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/24.jpg)
2424
Figure 9.11Figure 9.11Levels of recursive calls to mergesort given an array of eight items
![Page 25: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/25.jpg)
2525
Figure 9.12Figure 9.12A partition about a pivot
![Page 26: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/26.jpg)
2626
Figure 9.13Figure 9.13kSmall versus quicksort
![Page 27: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/27.jpg)
2727
Figure 9.14Figure 9.14Invariant for the partition algorithm
![Page 28: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/28.jpg)
2828
Figure 9.15Figure 9.15Initial state of the array
![Page 29: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/29.jpg)
2929
Figure 9.16Figure 9.16Moving theArray[firstUnknown] into S1 by swapping it with theArray[lastS1+1] and by incrementing both lastS1 and firstUnknown
![Page 30: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/30.jpg)
3030
Figure 9.17Figure 9.17Moving theArray[firstUnknown] into S2 by incrementing firstUnknown
![Page 31: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/31.jpg)
3131
Figure 9.18aFigure 9.18aDeveloping the first partition of an array when the pivot is the first item
![Page 32: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/32.jpg)
3232
Figure 9.18bFigure 9.18bDeveloping the first partition of an array when the pivot is the first item
![Page 33: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/33.jpg)
3333
Figure 9.19Figure 9.19A worst-case partitioning with quicksort
![Page 34: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/34.jpg)
3434
Figure 9.20Figure 9.20A average-case partitioning with quicksort
![Page 35: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/35.jpg)
3535
Figure 9.21Figure 9.21A radix sort of eight integers
![Page 36: 1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method for evaluation: Implement each as a method and then](https://reader030.vdocument.in/reader030/viewer/2022032800/56649d255503460f949fbfdf/html5/thumbnails/36.jpg)
3636
Figure 9.22Figure 9.22Approximate growth rates of time required for eight sorting algorithms