The code:
int temp = 0;
for (int h = 1; h <= n; h = h * 2)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j<=n; j++)
{
for (int k = 1; k<i; k++)
{
temp++
}
}
}
}
According to my calculations, big-O is lg n * n^4. Kindly guide.
Edit: Thank you all for the replies. I understood where I was making the mistake. I really appreciate everyone who participated.
Knightrider is basically right, but if you want correct numbers, you cant just say "I take the biggest value of
i
".Yes, it is correct in terms
f(n) = O(...)
, but you can also writef(n) = O(n^15)
and this will be correct too.The last loop is executed n-times, then n-1 times, then n-2 times etc., which is
n+n-1+n-2+n-3....+3+2+1
which isn(n+1)/2
. Now you can multiply it and getn(n+1)/2 = n^2/2 + n/2
, constants can be ignored in asymptotic operations, which meansn^2/2 + n/2 = Theta(n^2+n)
, which can be also transformed ton^2+n = Theta(n^2)
So after all, the result is not changed, BUT you have to be sure.
The final result is
n^3*log_2(n)
as described by knightriderThe
k
loop is executedi
times. The maximum value ofi
is n. so you can consider it as being executedn
times always (over approximation)complexity = ln(n) * n * n * n = ln*pow(n,3)
Another way to look at it... lets exchange i loop and j loop
now look at it.... the total number of executions of (i,k) is
n*(n+1)/2
. so now what is the complexity?h * j * (i,k) = ln(n) * n * (n*(n+1)/2) == ln(n) * pow(n,3)
Just for kicks, I ran this code with various values of n:
conclusion: log(n) * n^3
Analysing the algorithm's complexity using Sigma notation
For the sake of completeness: when analyzing the time complexity of nested and co-dependent loops such as in your algorithm, Sigma notation can be a good tool
where ⌊x⌋ and ⌈x⌉ are the floor and ceiling functions.
From the above, it's apparent that an upper bound on the asymptotic behaviour of the algorithm is
O(n^3 log_2(n))
.Using Sigma notation to estimate the actual number of iterations
Sigma notation analysis is, in addition to being a rigorous tool for Big-O(-Ω, -Θ) analysis, also useful if we're interested in counting or estimating the actual number of iterations of our algorithm.
We compare the estimated number of iterations—using the formula prior to the ≤ symbol above—with the actual number of iterations as presented in @JohnHascell:s answer.
We see that the count from the formula conforms perfectly with the actual count; in this case, the estimation is in fact an actual count.