This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 2 . Therefore the statement ∀ n ≥ N 1 10 n √ n ≤ c (100 n + 1000) is contradicted by taking n = max( N, 1 , 11000 2 c 2 ) (The reason we have 1 there is because our def of BigOh does not require N,c > 1.) Sample Problem 4 Analyze the following fragment of code and give a BigOh characterization of its running time (that is, give the best upper bound on the worstcase running time of the algorithm that you can find). Explain your analysis. public static void doWork (int arr) { for (int i=1; i < 10; i++) { for (int j=0; j < 3*i; j++) { for (int k=0; k < arr.length; k++) { System.out.print(i+j+k); } } } Answer Let n = arr . length be the size of the input. 2 The body of the kloop runs in time O (1). For each i,j the kloop executes n times therefore it runs in time O ( n · 1), that is O ( n ). Therefore the body of the jloop runs in time O ( n ). The jloop executes 3 i times so it runs in time O ((3 i ) n ), However, we always have i < 10 therefore the jloop executes in time O (30 n ) therefore in O ( n ). So the body of the iloop runs in time O ( n ) and it executes 10 times so the iloop (and therefor the entire method) runs in time O (10 n ) that is O ( n ). What happens if we replace 3 with arr.length ? 3...
View
Full Document
 Spring '09
 TANNEN
 Algorithms, Data Structures, Analysis of algorithms, Negative and nonnegative numbers, Existential quantification

Click to edit the document details