1 Chapter 4 Analysis Tools. 2 Which is faster – selection sort or insertion sort? Potential method...

36
1 Chapter 4 Chapter 4 Analysis Tools Analysis Tools
  • 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...

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

3636

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