CMPS 10
Fall 2007
Quiz 2
Solutions
1.
(6 Points) Suppose algorithms A and B solve the same problem.
On input of size
n
, algorithm A does
2
15
n
basic operations, while algorithm B does
3
)
05
(.
n
basic operations.
a.
(2 Points)
Which of the two algorithms is most efficient for large
n
?
Answer:
A is most efficient for large
n
b.
(2 Points)
For what value of
n
are the two algorithms equally efficient?
Solution:
3
2
)
05
(.
15
n
n
=
⇒
)
05
/(.
15
=
n
⇒
300
=
n
c.
(2 Points)
How many basic operations would be performed by either algorithm on input of the
size
n
you found in part (b)?
Solution:
000
,
350
,
1
)
300
(
15
2
=
⋅
basic operations
2.
(8 Points)
Trace the following algorithm.
1)
1
←
i
2)
while
6
≤
i
3)
1
←
j
4)
while
i
j
≤
5)
print
‘happy’
6)
1
+
←
j
j
7)
1
+
←
i
i
8)
stop
Determine the number of times line 5 is executed.
In other words, determine the number of
times the
word ‘happy’ will be printed.
Solution:
Observe that
when
1
=
i
, then
j
ranges from 1 to 1, hence line 5 is executed 1 time
when
2
=
i
, then
j
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '08
 TANTALO
 Natural number, Search algorithm, Basic Operations

Click to edit the document details