A Time Complexity Question
What is the time complexity of following function fun()? Assume that log(x) returns log value in base 2.
void fun() { int i, j; for (i=1; i<=n; i++) for (j=1; j<=log(i); j++) printf("GeeksforGeeks"); } |
chevron_right
filter_none
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.
Sources:
http://en.wikipedia.org/wiki/Stirling%27s_approximation
Recommended Posts:
- What does 'Space Complexity' mean?
- Time Complexity of building a heap
- Algorithm Practice Question for Beginners | Set 1
- An interesting time complexity question
- Time Complexity of Loop with Powers
- Time Complexity where loop variable is incremented by 1, 2, 3, 4 ..
- Time Complexity of a Loop when Loop variable “Expands or Shrinks” exponentially
- Performance of loops (A caching question)
- Understanding Time Complexity with Simple Examples
- Time complexity of recursive Fibonacci program
- Practice Questions on Time Complexity Analysis
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Time taken by Loop unrolling vs Normal loop
- Time Complexity Analysis | Tower Of Hanoi (Recursion)
- Knowing the complexity in competitive programming

