{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Suppose f n is o g n prove that f n 100 n is o g n n

Info iconThis preview shows pages 13–14. Sign up to view the full content.

View Full Document Right Arrow Icon
Suppose F ( n ) is O ( G ( n )). Prove that F ( n ) + 100 n is O ( G ( n ) + n ). 46. Consider the following Java code fragment static void swap( int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } static int foo( int j, int i) { int r = 1; for (int k = j; k < i; k++) r = (r * k) % 100 ; return r; } static void bar(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[ i ] = i+1; for (int i = 1; i < n; i++ ) swap( a, i, foo( 0, i) ); } Analyze the running time of bar in Big-Oh notation. 13
Background image of page 13

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

View Full Document Right Arrow Icon
47. In this problem you NOT allowed to use any of the theorems about Big-Oh stated in the lecture slides, the textbook, or the lab writeups. Your proof should rely only on the definition of Big-Oh. Prove that 2 n +1 is O (2 n ). 48. Consider the following code fragment for computing the nth Fibonacci number, F n . Let C ( n ) be the number of recursive calls made by this fragment when the input is n . Show that C ( n ) = C ( n - 1) + C ( n - 2) + 1. Prove using induction that C ( n ) > F n . public static long fib (int n) { if (n <= 1) return n; else return fib(n-1) + fib(n-2); } 49. Which of the following statements are true: (a) O ( n 3 + n 2 + n ) is the same as O ( n 2 ). (b) O (10 10 ) is different from O (1). (c) The running time of an algorithm depends on the underlying model of computation. (d) The worst-case running time of the following code fragment is O ( n 2 ): int x = 0; for (int i = 0; i < n; i++) if (false) for (int j = 0; j < i; j++) x++; 14
Background image of page 14
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}