This preview shows pages 1–3. Sign up to view the full content.
CS 161 Summer 2009
Homework #1 Sample Solutions
Regrade Policy:
If you believe an error has been made in the grading of your homework,
you may resubmit it for a regrade. If the error consists of more than an error in addition of
points, please include with your homework a detailed explanation of which problems you
think you deserve more points on and why. We reserve the right to regrade your entire
homework, so your ﬁnal grade may either increase or decrease.
Problem 1 [5 points]
For arrays of types like byte, char, double, ﬂoat, int, long, and short, java.util.Arrays uses
a quicksort algorithm. For arrays of the general Object type, it uses a mergesort algorithm.
The source of this information was
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html.
Problem 2 [10 points]
A
1
: Θ(
n
3
). You can see this by noticing that when
n
doubles, the data points grow by
a factor of 8 (which is 2
3
), meaning that when
x
grows by a factor of
n
,
f
(
x
) grows by a
factor of
n
3
, meaning this is Θ(
n
3
).
A
2
: Θ(1). Notice that no matter how
x
grows,
f
(
x
) does not vary signiﬁcantly, meaning
that the function does not depend on the growth rate of
x
, so it must be Θ(1).
A
3
: Θ(
n
). Notice that when n doubles, the data points also double, meaning that
f
(
x
)
grows as the same rate as
x
grows, so it must be Θ(
n
).
A
4
: Θ(log(
n
)). If you plot this on a log

log scale, you get a straight line, meaning this is
Θ(log(
n
)).
A
5
: Θ(
n
2
). You can see this by noticing that when
n
doubles, the data points grow by
a factor of 4 (which is 2
2
), meaning that when
x
grows by a factor of
n
,
f
(
x
) grows by a
factor of
n
2
, meaning this is Θ(
n
2
).
A
6
: Θ(
n
log
n
). When
n
doubles, the ratio
2
n
log(2
n
)
n
log
n
is between 2.2 and 2.14, gradually
declining when 1000
≤
n
≤
16000. That is close to the real data points.
1
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document 2
Alternative answer: Θ(
n
1
.
15
). Notice that when
n
doubles, the data points grow by a factor
of approximately 2
1
.
15
, meaning that when
x
grows by a factor of
n
,
f
(
x
) grows by a factor
of
n
1
.
15
, meaning this is Θ(
n
1
.
15
). You can ﬁgure out the factor of 1
.
15 by looking at pairs
of datapoints, and solving the equation factor =
log
(
y
2
/y
1
)
log
(
x
2
/x
1
)
, where (
x
1
,y
1
) and (
x
2
,y
2
) are
two datapoints. Solving this for each consecutive pairs of points in the data gives factors
of 1.13, 1.18, 1.11, 1.18, and 1.12, which are all around 1.15.
Problem 3 [10 points]
This is the end of the preview. Sign up
to
access the rest of the document.
This document was uploaded on 05/25/2011.
 Summer '09
 Algorithms

Click to edit the document details