05-AlgorithmAnalysis - 5-1Algorithm AnalysisRick MercerData...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 5-1Algorithm AnalysisRick MercerData Structures with Java Chapter 55-2Algorithm AnalysisObjectivesAnalyze the efficiency of algorithmsAnalyze a few classic algorithms:•Linear Search, Binary Search, Selection SortKnow the differences between O(1), O(n), O(logn), and O(n2)Visualize runtime differences with experiments5-3Algorithms continuedComputer Scientists focus on problems such asHow fast do algorithms runHow much memory does the process requireExample ApplicationsMake the Internet run faster•Pink-Degemark's routing algorithm•Gene Myersdetermined the sequences of the Drosophila, Human, and Mouse genomes using his whole genome shotgun technique5-4Analysis of Algorithms We have ways to compare algorithmsGenerally, 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 longerthe variable nsuggests the "number of things"If an algorithm requires 0.025n2+ 0.012n + 0.0005 seconds, just plug in a value for n 5-5A Computational ModelTo summarize algorithm runtimes, we can use a computer independent modelinstructions are executed sequentiallycount all assignments, comparisons, and increments there is infinite memoryevery simple instruction takes one unit of time5-6Simple InstructionsCount the simple instructionsassignments have cost of 1comparisons have a cost of 1let's count all parts of the loopfor(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+2each statement in the repeated part of a loop have have a cost equal to number of iterations5-7ExamplesCostsum = 0; -> 1 sum = sum + next; -> 1 Total Cost: 2Costfor( int i = 1; i <= n; i++ ) -> 1 + n+1 + n = 2n+2sum = sum++; -> n Total Cost: 3n + 2Costk = 0 -> 1for( int i = 0; i < n; i++ ) -> 2n+2for( int j = 0; j < n; j++ ) -> n(2n+2) = 2n2 +2nk++; -> n2 Total Cost: 3n2+ 4n + 35-8Total Cost of Sequential SearchCostBankAccount searchedForAccount = null; -> 1int index = 0; -> 1while( index < n && -> n!searchID.equals(accounts[index].getID())) {-> n index++;-> n}if( index < n ) -> 1searchedForAccount = accounts[index];-> 1// searchedForAccount is null or refers to the desired objectTotal cost = 3n+45-9Different CasesThe 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 caseIf searchID is found at index n-1:_____ iterations•worst caseWhat is typical?5-10Typical CaseThe average describes the more typical case....
View Full Document

This note was uploaded on 04/02/2008 for the course C SC 127b taught by Professor Mercer during the Spring '08 term at University of Arizona- Tucson.

Page1 / 49

05-AlgorithmAnalysis - 5-1Algorithm AnalysisRick MercerData...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online