consider a nested loop:
for i from 1 to n
k=i;
while(k>0)
do c operations;
k=floor[k/2];
end while
end for
to compute num of operations
I need to know how many iterations in the while loop first, I figured that(probably wrong):
k=i,k=floor[1/2*i],k=floor[1/4]….k=floor[(1/2)^j*i],k=1. I know the last k will always be 1 right? and the num of operation within the while loop is j+1? and I don’t know how to solve j.
Can someone help?
while loop loops for log(i) times. so inside one iteration of the for i loop, c * log(i) operations are done. so in total:
c*log(1) + c*log(2) + c*log(3) + … + c*log(n)
In case you need the asymptotic runtime: that is O(n log (n)).