Best and worst case running time of an algorithm -


i need calculate best , worst case running time of following algorithm. matter need consider i*i in second loop? , complexity of loop q(n^2)?

    i=1 2*n        j=1 i*i           k=1 k<j              if j%i==0                  sum=sum+1; 

this full code

    procedure proc(n:integer){     var i,j,k :integer     var sum: integer    begin  sum=0;  i=1 2*n  begin        j=1 i*i        begin                k=1 k<j               begin                        if j%i==0                        sum=sum+1;                        end if                        k=k+1;     end    j=j+1;   end   i=i+i;  end  end } 

let's analyze inside out.

         if j%i==0              sum=sum+1; 

this takes constant time every time reached.

      k=1 k<j 

the inner loop runs o(j) times, once each value of k 1 j (exclusive). far have o(j*1) = o(j)

   j=1 i*i 

for middle loop - each value of j has job in complexity of o(j). means, going need entire loop (for each value of i):

1 + 2 + .... + i*i = i*i*(i*i+1)/2 = i^2(i^2+1)/2  [sum of arithmetic progression] 

the above in o(i^4)

for i=1 2*n 

sum{i^2*(i^2+1)/2 | 1 2n } which in o(n^5)


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -