C.
Prove that
C
(
n
) is Θ(
D
(
n
)).
Answer
Since we have just shown that
C
(
n
) and
D
(
n
) are BigOh of each other it follows that
C
(
n
) is
Θ(
D
(
n
)).
Problem 5: 20 points
Analyze the following fragment of code and give a BigOh characterization of its running time (that
is, give the best upper bound on the worstcase running time of the algorithm that you can find).
Explain your analysis.
public static void doWork (int[] arr) {
for (int i=1; i < arr.length; i++) {
for (int j=0; j < i; j++) {
System.out.print(j);
}
}
}
Answer
Let
n
=
arr
.
length
be the size of the input.
The body of the inner loop runs in time
O
(1).
For each
i
= 1
, . . . , n

1 the inner loop executes
i
times therefore it runs in time
O
(
i
·
1), that is
O
(
i
). But
i < n
so the each of the executions runs in time
O
(
n
).
Therefore the body of the outer loop runs in time
O
(
n
).
The outer loop executes
n

1 times so it runs in time
O
((
n

1)
n
), that is,
O
(
n
2
). The whole method
runs in time
O
(
n
2
).
Note.
An alternative more detailed analysis computes the sum
n

1
X
i
=1
i
=
(
n

1)(
n

1 + 1)
2
=
n
2

n
2
but still, the (asymptotically) best we can state is that the method runs in time
O
(
n
2
).
3
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Problem 6: 20 points
Write a Java method that takes an argument an array
a
of type
int[]
and returns true if the
elements of
a
are in
strictly
increasing order and false otherwise. Then, analyze your method and
give a BigOh characterization of its running time (that is, give the best upper bound you can find
on the worstcase running time of your method). Explain your analysis.
Answer
One method that accomplishes this task goes as follows:
public static bool sorted(int[] arr) {
if (arr.length<2) return true;
for (int i=0; i<arr.length1; i++) {
if (arr[i]>=arr[i+1]) {
return false;
}
}
return true;
}
Let
n
=
arr
.
length
be the size of the input.
The worst case running time is when the array is sorted in strictly increasing order. In this case the
for
the loop iterates
n

1 times. The body of the loop runs in time
O
(1). Thus, we can conclude
that the method runs in time
O
(
n
).
Extra Credit 1: 15 points
In this problem you are NOT allowed to use the theorems about BigOh stated in the lecture notes.
Your proof should follow just from the definition of BigOh. (As usual in problems with BigOh, we
assume that the functions map nonnegative reals to strictly positive reals.)
Let
f
(
n
) be such that for any even
n
,
f
(
n
) =
n
2
and for any odd
n
,
f
(
n
) =
n
2
.
TYPO
We should not have formulated the problem like this because it does not give any information
about
f
(
n
) when
n
is not a natural number!
And it is quite possible that on arguments that are
not natural numbers
f
would behave asymptotically worse than
n
2
! We reformulate the problem as
follows:
Let
f
(
n
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '09
 TANNEN
 Algorithms, Data Structures, Negative and nonnegative numbers, 2k, doMoreWork

Click to edit the document details