0% found this document useful (0 votes)
207 views3 pages

Time Complexity

This document contains 10 algorithms and asks to determine the time complexity of each one. Algorithm 1 sums all elements of a matrix and has a time complexity of O(nm) as it has two nested for loops. Algorithm 2 finds the maximum element in an array and has a time complexity of O(n) as it has one for loop. Algorithm 10 calculates x to the power of n and has a time complexity of O(logn) as it recursively halves n each iteration.

Uploaded by

Fahad Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
207 views3 pages

Time Complexity

This document contains 10 algorithms and asks to determine the time complexity of each one. Algorithm 1 sums all elements of a matrix and has a time complexity of O(nm) as it has two nested for loops. Algorithm 2 finds the maximum element in an array and has a time complexity of O(n) as it has one for loop. Algorithm 10 calculates x to the power of n and has a time complexity of O(logn) as it recursively halves n each iteration.

Uploaded by

Fahad Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Deparmtent of Computer Science & Information Technology,

University of Engineering & Technology Peshawar.

Advance Analysis of Algorithms.


Spring 2014.
Dr. Iftikhar Ahmad.
Practice Questions : Time Complexity of Algorithms.

Note
Compute the time complexity of the following algorithms, some of the algorithms are without any
purpose, so do not try to figure out what they actually do. If you want to challenge yourself, find the
purpose of Algorithm 10.
Algorithm 1 Algorithm to find the sum of all elements of a matrix of size n x m
1: procedure MatElementsSum(A[0 . . . n 1, 0, . . . m 1])
2:
sum = 0
3:
for i = 0 to n 1 do
4:
for j = 0 to m 1 do
5:
sum = sum + A[i, j]
6:
end for
7:
end for
8:
return sum
9: end procedure

Algorithm 2 Algorithm to find the maximum in an array of size n


1: procedure FindMax(A[0 . . . n 1, 0])
2:
max = A[0]
3:
for i = 1 to n 1 do
4:
if max < A[i] then
5:
max = A[i]
6:
end if
7:
end for
8:
return max
9: end procedure

Dr. Iftikhar Ahmad ([email protected])

Algorithm 3 Algorithm which does something with an array of size n


1: procedure SomeAlg(A[0 . . . n 1, 0])
2:
var = 1
3:
i=n1
4:
while i 0 do
5:
var = var 2A[i]
6:
i=i1
7:
end while
8:
return var
9: end procedure

Algorithm 4 Algorithm which does something with an array of size n


1: procedure SomeAlg1(A[0 . . . n 1, 0])
2:
var = 1
3:
while i > n do
4:
var = var 2A[i]
5:
i=i+2
6:
end while
7:
return var
8: end procedure

Algorithm 5 Algorithm which does something with an array of size n


1: procedure SomeAlg2(A[0 . . . n 1, 0])
2:
var1 = 1
3:
var2 = 100
4:
for i = 1 to n 1 do
5:
j=i
6:
var2 = var2 + var1
7:
while j < n do
8:
var1 = var2 /2
9:
var2 = var2 var1 /2
10:
j =j+2
11:
end while
12:
end for
13: end procedure

Algorithm 6 Algorithm which just sums up for no apparent reason


1: procedure SomeAlg3(n)
2:
sum = 0

3:
for i = 1 to
n do
4:
sum = sum + i
5:
end for

6:
for i = 1 to
n/2 do
7:
sum = sum + i
8:
end for
9:
for i = 1 to 10 do
10:
sum = sum + i
11:
end for
12: end procedure

Dr. Iftikhar Ahmad ([email protected])

Algorithm 7 Algorithm which just sums up for no apparent reason


1: procedure SomeAlg4(n)
2:
sum = 0

n do
3:
for i = 1 to
4:
for j = 1 to 5 do
5:
sum = sum + i j
6:
end for
7:
end for
8: end procedure

Algorithm 8 Algorithm which just sums up for no apparent reason in a weird fashion
1: procedure SomeAlg5(n)
2:
sum = 0
3:
for i = 1 to 2n do
4:
for j = 1 to i i do
5:
for k = 1 to j do
6:
sum = sum + 1
7:
end for
8:
end for
9:
end for
10: end procedure

Algorithm 9 To calculate factorial of a given number n


1: procedure Factorial(n)
2:
if n = 1 then
3:
return 1
4:
else
5:
return n x Factorial(n 1)
6:
end if
7: end procedure

Algorithm 10 A Mystic Algorithm


1: procedure MysteryAlg(x , n)
2:
if n = 0 then
3:
return 1
4:
end if
5:
if n = 1 then
6:
return x
7:
end if
8:
if n is even then
9:
return MysteryAlg(x x, n/2)
10:
else
11:
return MysteryAlg(x x, n/2) x
12:
end if
13: end procedure

Dr. Iftikhar Ahmad ([email protected])

You might also like