Time Complexity

September 29, 2017 | Author: Fahad Khan | Category: Time Complexity, Theoretical Computer Science, Matrix (Mathematics), Computer Science, Algorithms
Share Embed Donate


Short Description

Time Complexity...

Description

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])

1

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=n−1 4: while i ≥ 0 do 5: var = var ∗ 2A[i] 6: i=i−1 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])

2

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])

3

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF