Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 1 Chapter 4 Analysis Tools

2 2 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

3 3 What are the most important criteria that influence our algorithm implementation choices? What do each of these criteria directly affect?

4 4 Experimental Studies Write a program implementing the algorithm Run the program with inputs of varying size and composition Use a method like System.currentTimeMillis() to get an accurate measure of the actual running time Plot the results

5 5 Limitations of Experiments It is necessary to implement the algorithm, which may be difficult Results may not be indicative of the running time on other inputs not included in the experiment. In order to compare two algorithms, the same hardware and software environments must be used

6 6 Theoretical Analysis Uses a high-level description of the algorithm instead of an implementation Characterizes running time as a function of the input size, n. Takes into account all possible inputs Allows us to evaluate the speed of an algorithm independent of the hardware/software environment

7 7 Big-O Two important rules: Make g(n) as small as possible g(n) never contains unnecessary terms Asymptotic Analysis The goal of asymptotic analysis is to determine the complexity order of an algorithm

8 8 Big-Oh Rules If is f(n) a polynomial of degree d, then f(n) is O(n d ), i.e., 1. Drop lower-order terms 2. Drop constant factors Use the smallest possible class of functions Say “ 2n is O(n) ” instead of “ 2n is O(n 2 ) ” Say “ 2n is O(n) ” instead of “ 2n is O(n 2 ) ” Use the simplest expression of the class Say “ 3n  5 is O(n) ” instead of “ 3n  5 is O(3n) ” Say “ 3n  5 is O(n) ” instead of “ 3n  5 is O(3n) ”

9 9

10 10 Figure 9.6 An insertion sort partitions the array into two regions

11 11 Figure 9.7 An insertion sort of an array of five integers.

12 12

13 13 Figure 9.4 A selection sort of an array of five integers

14 14 Order of Complexity Exponential Polynomial Log Linear Constant Exponential > Polynomial > Log > Linear > Constant

15 15 Figure 9.3a A comparison of growth-rate functions: a) in tabular form

16 16 Figure 9.3b A comparison of growth-rate functions: b) in graphical form

17 17

18 18

19 19 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

20 20 Figure 9.5 The first two passes of a bubble sort of an array of five integers: a) pass 1; b) pass 2

21 21 Figure 9.8 A mergesort with an auxiliary temporary array

22 22 Figure 9.9 A mergesort of an array of six integers

23 23 Figure 9.10 A worst-case instance of the merge step in mergesort

24 24 Figure 9.11 Levels of recursive calls to mergesort given an array of eight items

25 25 Figure 9.12 A partition about a pivot

26 26 Figure 9.13 kSmall versus quicksort

27 27 Figure 9.14 Invariant for the partition algorithm

28 28 Figure 9.15 Initial state of the array

29 29 Figure 9.16 Moving theArray[firstUnknown] into S 1 by swapping it with theArray[lastS1+1] and by incrementing both lastS1 and firstUnknown

30 30 Figure 9.17 Moving theArray[firstUnknown] into S 2 by incrementing firstUnknown

31 31 Figure 9.18a Developing the first partition of an array when the pivot is the first item

32 32 Figure 9.18b Developing the first partition of an array when the pivot is the first item

33 33 Figure 9.19 A worst-case partitioning with quicksort

34 34 Figure 9.20 A average-case partitioning with quicksort

35 35 Figure 9.21 A radix sort of eight integers

36 36 Figure 9.22 Approximate growth rates of time required for eight sorting algorithms


Download ppt "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."

Similar presentations


Ads by Google