data structures - time-complexity of nested loop -
why time-complexity of
function (n) { //this loop executes n times for( = 1 ; <= n ; + + ) //this loop executes j times j increase rate of for( j = 1 ; j <= n ; j+ = ) print( “*” ) ; } its running time n*(n^1/2)=n^3/2
so, o(n^3/2)
please explain mathematical steps.
thank in advance.
the running time bounded o(n^{3/2}), that's not tight!
notice inner loop makes o(n/i) iterations each i=1,2,...n), time complexity o(n * (1 + 1/2 + 1/3 + ... + 1/n)) = o(n log n).
this because 1+1/2+1/3+...+1/n=o(log n) well-known harmonic series.
Comments
Post a Comment