lab2soln

2 therefore the statement ∀ n ≥ n 1 10 n √ n

Info iconThis preview shows pages 2–3. 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 Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the 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 Big-Oh characterization of its running time (that is, give the best upper bound on the worst-case 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 k-loop runs in time O (1). For each i,j the k-loop executes n times therefore it runs in time O ( n · 1), that is O ( n ). Therefore the body of the j-loop runs in time O ( n ). The j-loop executes 3 i times so it runs in time O ((3 i ) n ), However, we always have i < 10 therefore the j-loop executes in time O (30 n ) therefore in O ( n ). So the body of the i-loop runs in time O ( n ) and it executes 10 times so the i-loop (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

{[ snackBarMessage ]}

Page2 / 3

2 Therefore the statement ∀ n ≥ N 1 10 n √ n ≤...

This preview shows document pages 2 - 3. Sign up to view the full document.

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