This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: public static String reverse1(String s) { int N = s.length(); String rev = ""; for (int i = 0; i < N; i++) rev = s.charAt(i) + rev; return rev; } 24. In this problem you NOT allowed to use any of the theorems about BigOh stated in the lecture slides, the textbook, or the lab writeups. Your proof should rely only on the definition of BigOh. Prove that n √ n is not O ( n ). 25. For each of the following statements, if true prove using only the definition of bigOh, if false give a counterexample. (As usual, the functions involved take strictly positive values.) (a) For any f ( n ), g ( n ) is O ( nf ( n )), where g ( n ) = ∑ n i =1 f ( i ). (b) For any f ( n ) ,g ( n ), if log f ( n ) is O (log g ( n )) then f ( n ) is O ( g ( n )). 26. Suppose 0 < f ( n ) < g ( n ) for all n ≥ 1000. For the following statement, if true , give a proof, and if false , give a counterexample. You can assume any of the theorems about BigOh stated in the lecture notes. n · f ( n ) + g ( n ) is O ( g ( n ) · log n ) 27. (20 pts) Assume that the concatenation of strings s1 + s2 takes O ( k ) time where k is the size of the longest of s 1 and s 2 . Assume also that the method call s.substring(i, j) takes O ( j i ) time. Everything else without method calls, as usual, takes constant time. Find a BigOh bound for the worstcase running time of the method below by writing the recurrence relation and solving it for N = 2 m . Write the result in terms of N (not m ). public static String reverse2(String s) { int N = s.length(); if (N <= 1) return s; String left = s.substring(0, N/2); String right = s.substring(N/2, N); String revright = reverse2(right); String revleft = reverse2(left); return (revright+revleft); } 28. Consider the following: 9 static int foo(char a) { for (int i = a.length1; i > 0; i=i/2) if (a[i] == ’@’) return 2; return 3; } static void bar(char b) { for (int j = 0; j < b.length; j=j+foo(b)) b[j]=0; } Analyze the worstcase running time of bar(b) as a function of n = b.length and give a BigTheta (Θ( ... )) bound. For code fragments whose running time is a constant, do not give a specific number of steps, use symbolic constants c 1 ,c 2 , etc. instead. 29. In this problem you NOT allowed to use any of the theorems about BigOh stated in the lecture slides, the textbook, or the lab writeups. Your proof should rely only on the definition of BigOh. Prove that 2 2 n is not O (2 n ). 30. In this problem you NOT allowed to use any of the theorems about BigOh stated in the lecture slides, the textbook, or the lab writeups. Your proof should rely only on the definition of BigOh. Suppose 0 < f ( n ) < g ( n ) for all n > 1000. Suppose also that g ( n ) is O ( n 2 ). Let h ( n ) = f ( n ) p g ( n )....
View
Full Document
 Spring '09
 TANNEN
 Algorithms, Data Structures, Big O notation, Analysis of algorithms, Binary heap, Tree traversal, static int foo

Click to edit the document details