Stuck with me HW – Need to try complexity
time=0;
for (i=n; i>=1; i = sqrt(i))
for (j=1; j<=i; j++)
time++;
What I did – First loop going like this:
i=n, n^(1/2), n^(1/4)…1
than we get:
n^(1/2)^k = 1
if I log both sides one side get 0… what should I do?
I suppose there is a typo somewhere because otherwise it’s Θ(∞) if the input
nis not smaller than 1. (Fori == 1, the updatei = sqrt(i)doesn’t changei, so that’s an infinite loop.)So let us suppose it’s actually
Then, to get the complexity of nested loops, you need to sum the complexity of the inner loop for each iteration of the outer loop. Here, the inner loop runs
itimes, obviously, so we need to sum the valuesiruns through in the outer loop. These values aren, n^0.5, n^0.25, ..., n^(1/2^k), wherekis characterised byor, equivalently,
Now it remains to estimate the sum from above and below to get the Θ of the algorithm. This estimate is very easy if you start writing down the sums for a few (large) values of
n.