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 BigOh notation.
13
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
47. 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
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(n1) + fib(n2);
}
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 worstcase 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
This is the end of the preview.
Sign up
to
access the rest of the 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