You are on page 1of 4

Excercise 2

Hoang Nguyen d117721, Huy Nguyen d117724, Rao Moueed Ahmed d119447
September 20, 2020

1 Exercise 8

1
2 Exercise 9

2
3 Exercise 10

4 Exercise 11
If we always select the first item in the array T [1 : n] as the pivot item, and the input has already been
arranged in sorted (or in reverse ) order, the after each recursive call, the problem is divided into 2 sub-
problems with 0 and n-1 element respectively (unbalance). In addition, the partition costs Θ(n) time. The
recurrence for running time is:

T (n) = T (n − 1) + T (0) + Θ(n) = T (n − 1) + Θ(n)

The above equation has a solution T (n) = Θ(n2 ). The complexity is Θ(n2 )

5 Exercise 12
Algorithm 1: printLots(L, P):
item = L.head;
i = j = 0;
while i < P.length do
while j < P [i] and item 6= N U LL do
j+ = 1;
item = item.next;
if item 6= N U LL then
P rint(item.key);
i+ = 1;
else
break;

3
6 Exercise 13
Algorithm 2: SAME(A, B):
a = A.head;
b = B.head;
while a 6= N U LL and b 6= N U LL do
if a.key 6= b.key then
return F alse;
a = a.next;
b = b.next;
return (a = N U LL and b = N U LL);

You might also like