{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

analysis

# analysis - Running Time Analysis of Algorithms As soon as...

This preview shows pages 1–4. Sign up to view the full content.

Robert Sedgewick and Kevin Wayne • Copyright © 2005 • http://www.Princeton.EDU/~cos226 Analysis of Algorithms 2 Running Time Charles Babbage (1864) As soon as an Analytic Engine exists, it will necessarily guide the future course of the science. Whenever any result is sought by its aid, the question will arise - By what course of calculation can these results be arrived at by the machine in the shortest time? - Charles Babbage Analytic Engine (schematic) 3 Overview Analysis of algorithms. Framework for comparing algorithms and predicting performance. Scientific method. ! Observe some feature of the universe. ! Hypothesize a model that is consistent with observation. ! Predict events using the hypothesis. ! Verify the predictions by making further observations. ! Validate the theory by repeating the previous steps until the hypothesis agrees with the observations. Universe = computer itself. 4 Case Study: Sorting Sorting problem: ! Given N items, rearrange them in ascending order. ! Applications: statistics, databases, data compression, computational biology, computer graphics, scientific computing, ... Hanley Haskell Hauser Hayes Hong Hornet Hsu Hauser Hong Hsu Hayes Haskell Hanley Hornet

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
5 Insertion sort. ! Brute-force sorting solution. ! Move left-to-right through array. ! Exchange next element with larger elements to its left, one-by-one. Insertion Sort public static void insertionSort ( double [] a ) { int N = a . length ; for ( int i = 0 ; i < N ; i ++) { for ( int j = i ; j > 0 ; j --) { if ( less ( a [ j ], a [ j - 1 ])) exch ( a , j , j - 1 ); else break ; } } } 6 Insertion Sort: Observation Observe and tabulate running time for various values of N. ! Data source: N random numbers between 0 and 1. 400 million 40,000 99 million 20,000 25 million 10,000 6.2 million 5,000 Comparisons N 16 million 80,000 7 Data analysis. Plot # comparisons vs. input size on log-log scale. Regression. Fit line through data points ! a N b . Hypothesis. # comparisons grows quadratically with input size ! N 2 /4. Insertion Sort: Experimental Hypothesis slope 8 Insertion Sort: Prediction and Verification Experimental hypothesis. # comparisons ! N 2 /4. Prediction. 400 million comparisons for N = 40,000. Observations. Prediction. 10 billion comparisons for N = 200,000. Observation. 9.997 billion 200,000 Comparisons N 399.7 million 40,000 401.6 million 40,000 400.0 million 40,000 Comparisons N 401.3 million 40,000 Agrees. Agrees.
9 Insertion Sort: Theoretical Hypothesis Experimental hypothesis.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 8

analysis - Running Time Analysis of Algorithms As soon as...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online