This preview shows pages 1–11. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 51Algorithm AnalysisRick MercerData Structures with Java Chapter 552Algorithm 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 experiments53Algorithms continuedComputer Scientists focus on problems such asHow fast do algorithms runHow much memory does the process requireExample ApplicationsMake the Internet run faster•PinkDegemark's routing algorithm•Gene Myersdetermined the sequences of the Drosophila, Human, and Mouse genomes using his whole genome shotgun technique54Analysis 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 55A 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 time56Simple 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 iterations57ExamplesCostsum = 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 + 358Total 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+459Different 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 n1:_____ iterations•worst caseWhat is typical?510Typical 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.
 Spring '08
 Mercer

Click to edit the document details