I have calculated the time complexity of the following C function and it is coming to theta (nlogn)
.Can you tell me whether i am wrong,the answer given was theta(n^2logn)
?I have just started reading about these concepts.
int unk(int n)
{
int i,j,k=0;
for(i=n/2;i<=n;i++)
for(j=2;j<=n;j=j*2)
k=k+(n/2);
return k;
}
What I have done is:The outer loop executes for (n/2+2)
times,inner loop executes for (n/2+1)(logn+1)
times and the statement in the body of the loop executes (n/2+1)(logn)
times.So the total running time comes to theta(nlogn)
.(Assume all the costs to be 1 and the log to be a binary logarithm).