A Time Complexity Question

What is the time complexity of following function fun()? Assume that log(x) returns log value in base 2. 

C

filter_none

edit
close

play_arrow

link
brightness_4
code

void fun()
{
    int i, j;
    for (i = 1; i <= n; i++)
        for (j = 1; j <= log(i); j++)
            printf("GeeksforGeeks");
}

chevron_right


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(n*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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Improved By : vroghelia6

Article Tags :

34


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.