You are on page 1of 7

Excercise 1

Hoang Nguyen d117721, Huy Nguyen d117724


September 14, 2020

1 Exercise 1
a) The while loop execute in n loops, at the begining the program declares variable i. Futhermore, in each
loop it runs 2 command lines . Hence, f (n) = 1 + 2n, the order of magnitude of execution time is 1.
b) In this statement block, there is an outer loop and an inner loop inside of it. For the outer loop, it loops
n times for i from 0 to n − 1. For each loop i of outer loop, the inner loop runs x = x + 1 from j = i to
j = n − 1, namely n − i times . Hence
i=n−1
X (n − 1)n n2 + n
f (n) = n − i = n2 − =
i=0
2 2

Hence, the order of magnitude is 2.

2 Exercise 2
a) There are 3 nested loops. The first one runs i from 0 to n − 1. The second runs j from 0 to i − 1 and the
last one also runs k from 0 to i − 1 and x = x + 1 is executed. Hence, the number of times that x = x + 1 is
executed is
n−1 i−1 n−1
XX X n(n − 1)(2n − 1)
i− = i2 =
i=0 j=0 i=0
6

The order of magnitude is 3.


b)There are 3 nested loops. The first one runs i from 0 to n − 1. The second runs j from 0 to i and the last
one also runs k from 0 to j − 1 and x = x + 1 is executed. Hence, the number of times that x = x + 1 is
executed is
n−1 i n−1
XX X i(i + 1) n(n + 1)(n − 1)
j= =
i=0 j=0 i=0
2 6

The order of magnitude is 3.


c) There are 3 nested loops. The first one runs i from 1 to n, the increment of each step is 2, so we can
ignore all even values of i, we only consider odd values of i. The second runs j from 1 to i and the last one
also runs k from 1 to i and x = x + 1 is executed. Hence, the number of times that x = x + 1 is executed is
(n−1)/2 2i+1 (n−1)/2
X X X n(n + 1)(n + 2)
(2i + 1) = (2i + 1)2 = 12 + 32 + ... + n2 =
i=0 j=1 i=0
6

The order of magnitude is 3.

1
3 Exercise 3
The result would be Selection sort.
Algorithm 1: Selection-Sort(A):
for i from 0 to A.length do
minindex = i;
for j from i+1 to A.length do
if A[minindex] > A[j] then
minindex = j;
SWAP(A[i], A[minindex]);
return A;
From the pseudocode above, we can see the number of comparisons of Selection Sort is still quaradic, but
the number of swaps is less than that required by Bubble Sort. In some cases, for example, the elements are
strings (which is expensive to swap), Selection Sort would be better.

2
4 Exercise 4

3
4
5
5 Exercise 5
Algorithm 2: Counting-Sort(A, k):
Declare array B=[1....A.length] ;
Declare array C=[0.... k];
for i from 0 to k do
C[i]=0;
for j from 1 to A.length do
C[A[j]]=C[A[j]]+1;
//C[i] now contains number of element equal to i;
for i from 1 to k do
C[i]=C[i-1]+C[i];
//C[i] now contains number of element less than or equal to i;
for i from A.length down to 1 do
B[C[A[i]]]=A[i];
C[A[i]]= C[A[i]]-1;
return B;
Proof the correctness of this algorithm:
We will prove that if A[i] > A[j] then in the result, the index of A[i] must be greater than index of A[j]. In
the algorithm, we have declare the new array C, after the first for loop, C[i] contains the number of elements
of A equal i. After the second for loop, it is easy to see by induction that C[i] is the numbers of elements
less than or equal i, hence C[A[i]] > C[A[j]]. In the final for loop where we construct the output array B,
we set C[A[i]] as the index of element A[i] in B, since C[A[i]] > C[A[j]] and A[i] > A[j], our claim is proven.
In the final for loop, we decremented A[i] to ensure the algorithm works in the case A[i] = A[j] (then we
also have C[A[i]] > C[A[j]]). The time complexity is either constant (if n < k) or linear (if n > k)

6 Exercise 6

6
7 Exercise 7
a)
Algorithm 3: RecursiveAlgorithm(n):
if n==1 or n==2 then
return 1;
return RecursiveAlgorithm(n-1) + RecursiveAlgorithm(n-2)
b)
Algorithm 4: NonRecursiveAlgorithm(n):
if n==1 or n==2 then
return 1;
Declare array F[1....n];
F[1] = F[2] = 1 ;
for i from 3 to n do
F[i] = F[i-1] + F[i-2];
return F[n];
c) Since the time complexity for Recursive Algorithm is O(2n ), so the number of summations for it when
computing F (10) is 210 = 1024. On the other hand, the time complexity for Non-Recursive algorithm is
O(n), hence the number of summations for it when computing F (10) is 10.

You might also like