Professional Documents
Culture Documents
Exam
Exam
Solution
i) for i = 1 to n Do
A[i] = 1 / i
B[i] = i2
endfor
InsertionSort(A, n)
BubbleSort(B, n)
P a g e 1 | 16
Algorithm analysis questions | Ahmed Elrefaiy
ii) i = 1
while i < n do
i=2×i
endwhile
j =1
while j < 10 do
j=2×j
endwhile
Solution
i) O(n) + O(n2) + O(n2) = O(n2)
ii) O(log n) + O(1) = O(log n)
i) T(n) = 4T(n/2) + n
ii) T(n) = T(n/2) + n
Solution
P a g e 2 | 16
Algorithm analysis questions | Ahmed Elrefaiy
4)
Solution
P a g e 3 | 16
Algorithm analysis questions | Ahmed Elrefaiy
7)
Solution: C
8)
Solution: B
P a g e 4 | 16
Algorithm analysis questions | Ahmed Elrefaiy
9)
Solution: D
10)
Solution: D
P a g e 5 | 16
Algorithm analysis questions | Ahmed Elrefaiy
11)
Solution: C
12)
Solution: B
P a g e 6 | 16
Algorithm analysis questions | Ahmed Elrefaiy
13)
Solution: 3
14)
Solution: 4
P a g e 7 | 16
Algorithm analysis questions | Ahmed Elrefaiy
15)
Solution: 2
16)
Solution: 4
P a g e 8 | 16
Algorithm analysis questions | Ahmed Elrefaiy
17)
Solution: A
18)
Note that: 2 and 8 are constant. You can ignore them to be log n VS log n. So,
it will be O, Ω, θ
P a g e 9 | 16
Algorithm analysis questions | Ahmed Elrefaiy
19)
Solution: B
20)
Solution: B
P a g e 10 | 16
Algorithm analysis questions | Ahmed Elrefaiy
21)
Reason: this part log2 17log2n can be equal = log2 nlog217 . This is rule in
logarithmic where 17 and n can be swiped together. So, the two terms are
equal, which mean it can be O, Ω, θ.
22)
P a g e 11 | 16
Algorithm analysis questions | Ahmed Elrefaiy
Solution: C
23)
Solution: D
P a g e 12 | 16
Algorithm analysis questions | Ahmed Elrefaiy
24)
Solution: A
Reason: solve by tree method and sum time of each level will be equal
= n + 2n-2 + 4n-8 + 8n-24 + …… [Simplify this]
= n + 21(n-1) + 22(n-2) + 23(n-3) + ……
T(n) = ∑ 2𝑖 (n − i)
𝑖=1
25)
P a g e 13 | 16
Algorithm analysis questions | Ahmed Elrefaiy
Solution: C
26)
Solution: C
Reason: you can use master method to get that time complexity here is nlogn.
So, A B D are correct. C only is wrong.
P a g e 14 | 16
Algorithm analysis questions | Ahmed Elrefaiy
27)
Solution: D
Reason: T(n) = 𝑇(√𝑛 ) + 1
Review master method video part for the same example which = log log n
28)
Solution: C
P a g e 15 | 16
Algorithm analysis questions | Ahmed Elrefaiy
P a g e 16 | 16