{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

05-AlgorithmAnalysis

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

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

5-1 Algorithm Analysis Rick Mercer Data Structures with Java Chapter 5

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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
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?

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

View Full Document
5-10 Typical Case The average describes the more typical case.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}