{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

05-AlgorithmAnalysis

05-AlgorithmAnalysis - 5-1 Data Structures with Java...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
5-1 Algorithm Analysis Rick Mercer Data Structures with Java Chapter 5
Background image of page 1

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

View Full Document Right Arrow Icon
5-2 Algorithm Analysis Objectives Analyze the efficiency of algorithms Analyze a few classic algorithms: Linear Search, Binary Search, Selection Sort Know the differences between O(1), O(n), O(logn), and O(n 2 ) Visualize runtime differences with experiments
Background image of page 2
5-3 Algorithms continued Computer Scientists focus on problems such as How fast do algorithms run How much memory does the process require Example Applications Make the Internet run faster Pink-Degemark's routing algorithm Gene Myers determined the sequences of the Drosophila, Human, and Mouse genomes using his whole genome shotgun technique
Background image of page 3

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

View Full Document Right Arrow Icon
5-4 Analysis of Algorithms We have ways to compare algorithms Generally, the larger the problem, the longer it takes the algorithm to complete Sorting 100,000 elements can take much more time than sorting 1,000 elements and more than 10 times longer the variable n suggests the " n umber of things" If an algorithm requires 0.025n 2 + 0.012n + 0.0005 seconds, just plug in a value for n
Background image of page 4
5-5 A Computational Model To summarize algorithm runtimes, we can use a computer independent model instructions are executed sequentially count all assignments, comparisons, and increments there is infinite memory every simple instruction takes one unit of time
Background image of page 5

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

View Full Document Right Arrow Icon
5-6 Simple Instructions Count the simple instructions assignments have cost of 1 comparisons have a cost of 1 let's count all parts of the loop for(int j = 0; j < n; j++) j=0 has a cost of 1, j<n executes n+1 times,and j++ executes n times for a total cost of 2n+2 each statement in the repeated part of a loop have have a cost equal to number of iterations
Background image of page 6
5-7 Examples Cost sum = 0; -> 1 sum = sum + next; -> 1 Total Cost: 2 Cost for( int i = 1; i <= n; i++ ) -> 1 + n+1 + n = 2n+2 sum = sum++; -> n Total Cost: 3n + 2 Cost k = 0 -> 1 for( int i = 0; i < n; i++ ) -> 2n+2 for( int j = 0; j < n; j++ ) -> n(2n+2) = 2n 2 +2n k++; -> n 2 Total Cost: 3n 2 + 4n + 3
Background image of page 7

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

View Full Document Right Arrow Icon
5-8 Total Cost of Sequential Search Cost BankAccount searchedForAccount = null; -> 1 int index = 0; -> 1 while( index < n && -> n !searchID.equals(accounts[index].getID())) { -> n index++; -> n } if( index < n ) -> 1 searchedForAccount = accounts[index]; -> 1 // searchedForAccount is null or refers to the desired object Total cost = 3n+4
Background image of page 8
5-9 Different Cases The total cost of sequential search is 3n + 4 But is it always exactly 3n + 4 instructions? The last assignment does not always execute But does one assignment really matter? How many times will the loop actually execute? that depends If searchID is found at index 0: _____ iterations best case If searchID is found at index n-1:_____ iterations worst case What is typical?
Background image of page 9

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

View Full Document Right Arrow Icon
5-10 Typical Case The average describes the more typical case.
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}