Professional Documents
Culture Documents
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
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
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