You are on page 1of 5

NAME: Hesham Khalawi ID: 201783050 Section: 01

Q1:
1) The given array:

4 18
i 1 30 7 8 3 11 25 16 10

2) After the first iteration (No swap)

4 18 1i 30 7 8 3 11 25 16 10

3) After the second iteration

1 4 18 30
i 7 8 3 11 25 16 10

4) After the third iteration (No swap)

1 4 18 30 7i 8 3 11 25 16 10

5) After the fourth iteration

1 4 7 18 30 8i 3 11 25 16 10

6) After the fifth iteration


i
1 4 7 8 18 30 3 11 25 16 10
7) After the sixth iteration

1 3 4 7 8 18 30 11
i 25 16 10

8) After the seventh iteration


i
1 3 4 7 8 11 18 30 25 16 10

9) After the eighth iteration


i
1 3 4 7 8 11 18 25 30 16 10

10) After the ninth iteration

i
1 3 4 7 8 11 16 18 25 30 10

11) After the tenth iteration

1 3 4 7 8 10 11 16 18 25 30

Q2)
1) The given array:

2 17 19 5 13 11 4 8 15 12 7

2) Divide it into one-piece sub arrays (I + s ≮ n):

2 17 19 5 13 11 4 8 15 12 7
3) Merge each pair of sequences (i + s < n)
2 17 19 5 13 11 4 8 15 12 7

4) Merge each pair of sequences (i + s ≮ n)


2 5 17 19 4 8 11 13 15 12 7

5) Merge each pair of sequences (i + s < n)

2 4 5 8 11 13 17 19 7 12 15

6) Merge final pair of sequences:

2 4 5 7 8 11 12 13 15 17 19

Q3 a) Given 7 n3 +10 nlogn


 7 n3 +10 nlogn ≤ cg ( n ) , Take g ( n )=n3
 7 n3 +10 nlogn ≤ c n3
 Divide by n 3
10 logn
 7+ ≤c
n2
 For c = 17 (10 + 7) and n 0 ≥ 1, the inequality holds
 Hence, 7 n3 +10 nlogn=O(n 3)
b) Given 3 n2 +1000 n √ n
 3 n2 +1000 n √ n≤ cg ( n ) ,Take g ( n )=n 2
 3 n2 +1000 n √ n≤ c n2
 Divide by n 2
10 00 √ n
 3+ 2
≤c
n
 For c = 1003 (3 + 1000) and n 0 ≥ 1, the inequality holds
 Hence, 3 n2 +1000 n √ nis∈O(n 2)
n k

Q4 a) Given ∑ 1 ∑ 1
k =1 j=1

 Using the formula ∑ c=C (n−m+1):


j =m
n n
 ∑ 1 ( k −1+1 )=∑ k
k =1 k=1
n
n ( n+1 )
 Using the formula ∑ i= :
i=1 2
n 2
n +n
 ∑ k=
k =1 2
n

b) Given ∑ n−k
k =1

n n
 ∑ n−∑ k
k =1 k=1
2
n +n
 n2 −
2
n

c) Given ∑ k
i=1

n
 ∑ k=nk
i=1

Q5 a)
 For the outer loop, K loops for (Assume X is the iterator):
K k k
X= 0
, 1 … logn
2 2 2
 X = 2 ,2
logn logn−1
, … , 2 (We stop at 1 since the loop doesn’t accept 2^0)
1

 X = 2i , i = logX
 The summation cost:
logn n logn
 ∑ ∑ 1 = ∑ n−2i +1
i=1 j=2i i=1
logn−1

 Rewrite the summation as: ∑ i


n−2 +1
i=0

 n(logn) -( 2 −1 ) + logn – 1logn

 n(logn) -( 2logn ) + logn


Q5 b)
 The Big-o of this function is O(nlogn)

You might also like