Algorithms | Analysis of Algorithms | Question 2
What is the time complexity of fun()?
(A) Theta (n)
(B) Theta (n^2)
(C) Theta (n*Logn)
(D) Theta (nLognLogn)
Explanation: The time complexity can be calculated by counting number of times the expression “count = count + 1;” is executed. The expression is executed 0 + 1 + 2 + 3 + 4 + …. + (n-1) times.
Time complexity = Theta(0 + 1 + 2 + 3 + .. + n-1) = Theta (n*(n-1)/2) = Theta(n2)
Quiz of this Question