Algorithm Complexity-Nested For Loops -


for(j=n; j>1; j/=2)   ++p; for(k=1; k<p; k*=2)     ++q; 

on first code sample, p variable belong 1st loop. so,even not nested loop,will 2nd return log(n),too? mean totally, o(loglog(n))?

for(i=n; i>0; i--){   for(j=1; j<n; j*=2){     for(k=0; k<j; k++){       //statements-o(1)     }   } } 

and these one, nested k variable belong 2nd loop. so, should similar 1st one? o(n^2.log(n)) or o(n.log^2(n))?

  1. algorithm: first loop takes log(n) time. second loop takes log(log(n)) time. have log(n) + log(log(n)). first loop counts more. it's o(log(n)).

  2. algorithm: @ first runtime have when analyse 2 outer loops (means n log(n)). unfortunately there comes inner third loop makes more complex. third loop counts 0 2^m m=log(n). have sum 2^m 0 log(n)-1 n-1. n-1 run time of 2 inner loops. have multiply linear run time of outer loop. it's n(n-1) n²-n. have o(n²) 3 loops.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -