algorithm - complexity of program having two outer for loop running n times and one for loop running 4 times inside.second for loop -
for($j=1;$j<=$n;$j++) { for($k=1;$k<=4;$k++) { # o(1) operation } }
for have find out o(n) times constant loop run 4n times.
so in case follow same logic , has got 1 loop , means within run 4n times + outer loop:
for($i=1;$i<=$n;$i++) { for($j=1;$j<=$n;$j++) { for($k=1;$k<=4;$k++) { #o(1) operation } } } will o(n^2) or o(n^2)+o(4)??
it's o(n2). since 4 constant independent of n, not alter result of big-o.
of course of study 3rd nested loop create programme run slower. however, since slowdown constant factor, asymptotic timing of programme expressed in big-o notation not change.
algorithm
No comments:
Post a Comment