Logic Cycle Time Complexity

What is the complexity of the cycle that goes through this

for (int i = 0; i < n; i++)
{
    for (int j = 0; j < log(i); j++)
    {
        // do something
    }
}

In my opinion, the inner loop will work log(1)+log(2)+log(3)+...+log(n)once, so how can I calculate its complexity?

+4
source share
3 answers

So you have the amount log(1) + log(2) + log(3) + ... + log(n) = log(n!). Using the Stirling approximation and the fact that ln(x) = log(x) / log(e)one can obtain

log(n!) = log(e) * ln(n!) = log(e) (n ln(n) - n + O(ln(n)))

which gives the same complexity O(n ln(n))as the other answer (with a slightly better understanding of related constants).

+8
source

, "" . do_something, O(1) log N, log N. O(N log N). , .

: , " -" O(1) ( N, , ).

+8

log(1)+log(2)+log(3)+...+log(n). log(n/2) = log(n) - log(2). , n / 2 * (log(n) - log(2)) = Omega(nlog(n)). , n , log(n), O(nlog(n)).

0

Source: https://habr.com/ru/post/1650657/


All Articles