Homework 1 Solutions
CSCI-2300: Data Structures and Algorithms
Spring 2007
1. 2.1) Order the following functions by growth rate:
N
,
√
N
,
N
1
.
5
,
N
2
,
N
log
N
,
N
log log
N
,
N
log
2
N
,
N
log(
N
2
), 2
/N
, 2
N
, 2
N/
2
, 37,
N
2
log
N
,
N
3
. Indicate which functions grow at the
same rate.
Solution:
2
/N
, 37,
√
N
,
N
,
N
log log
N
,
N
log
N
,
N
log(
N
2
),
N
log
2
N
,
N
1
.
5
,
N
2
,
N
2
log
N
,
N
3
, 2
N/
2
, 2
N
.
N
log
N
and
N
log(
N
2
) grow at the same rate.
2. 2.2) Suppose
T
1
(
N
) =
O
(
f
(
N
)) and
T
2
(
N
) =
O
(
f
(
N
)). Which of the following are true?
(a)
T
1
(
N
) +
T
2
(
N
) =
O
(
f
(
N
))
(b)
T
1
(
N
)
T
2
(
N
)
=
O
(1)
(c)
T
1
(
N
) =
O
(
T
2
(
N
))
Solution:
(a) True.
(b) False. A counterexample is
T
1
(
N
) =
N
2
,
T
2
(
N
) =
N
, and
f
(
N
) =
N
2
.
(c) False. The same counterexample applies.
3. 2.7) For each of the following six program fragments:
(a) Give an analysis of the running time (Big-Oh will do).
(b) Implement the code in the language of your choice, and give the running time for several
values of
N
(c) Compare your analysis with the actual running times