What is the time complexity of following function fun()? Assume that log(x) returns log value in base 2.
Time Complexity of the above function can be written as Θ(log 1) + Θ(log 2) + Θ(log 3) + . . . . + Θ(log n) which is Θ (log n!)
Order of growth of ‘log n!’ and ‘n log n’ is same for large values of n, i.e., Θ (log n!) = Θ(n log n). So time complexity of fun() is Θ(n log n).
The expression Θ(log n!) = Θ(n log n) can be easily derived from following Stirling’s approximation (or Stirling’s formula).
log n! = n log n - n + O(log(n))
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Practice Questions on Time Complexity Analysis
- Understanding Time Complexity with Simple Examples
- Pseudo-polynomial Algorithms
- Time Complexity where loop variable is incremented by 1, 2, 3, 4 ..
- Time Complexity of Loop with Powers
- Performance of loops (A caching question)
- An interesting time complexity question
- Analysis of Algorithm | Set 4 (Solving Recurrences)
- Analysis of Algorithms | Set 4 (Analysis of Loops)
- Analysis of Algorithms | Set 3 (Asymptotic Notations)
- NP-Completeness | Set 1 (Introduction)
- Analysis of Algorithms | Set 2 (Worst, Average and Best Cases)
- Analysis of Algorithms | Set 1 (Asymptotic Analysis)
- What does 'Space Complexity' mean?
- Time Complexity of building a heap