This preview shows pages 1–3. Sign up to view the full content.
Practice Questions
1. Which of the following are true?
(a) 3n = O(n
2
)
(b) 3n
2
= O(nlgn)
(c) nlgn =
Θ
(20000nlgn)
(d) 2
n
=
Ω
(n
1000
)
Answers: (a), (c), (d)
(a) n
2
clearly grows larger than 3n as n gets large.
(c) 20000 is a constant in front of nlgn
(d) All exponential functions grow faster than all polynomial
functions.
(b) is false because 3n
2
grows faster than nlgn.
2.
What is the runtime of the following segment of code in
terms of n? Give an upper bound and justify it. Only a tight
upper bound will be accepted as a correct answer.
int i=1;
while (i <= n) {
int j = i;
while (j > 0)
j = j/2;
i++;
}
The outer loop runs n times. The inner loop will run log i times
at most since there is repeated halving. Since i never exceeds n,
it is safe to say the inner loop runs at most log n times. Hence
an upper bound on the run time is O(nlgn).
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document 3. Determine the following sum:
∑

=
1
0
3
n
i
i
This is a geometric sum with a first term of 1, with a common
ratio of 3, with n terms. Here's the sum:
This is the end of the preview. Sign up
to
access the rest of the document.