# Public static string reverse1(string s int n =

This preview shows pages 9–11. Sign up to view the full content.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the 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 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 n √ n is not O ( n ). 25. For each of the following statements, if true prove using only the definition of big-Oh, 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 Big-Oh 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 Big-Oh bound for the worst-case 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.length-1; 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 worst-case running time of bar(b) as a function of n = b.length and give a Big-Theta (Θ( ... )) 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 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 2 n is not O (2 n ). 30. 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. 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

{[ snackBarMessage ]}

### Page9 / 14

public static String reverse1(String s int N = s.length...

This preview shows document pages 9 - 11. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online