for($j=1;$j<=$n;$j++)
{
for($k=1;$k<=4;$k++)
{
# o(1) operation
}
}
for this what i have find out is this will be O(n) times as the constant for loop will run 4n times.
So in this case will it follow the same logic , as it has got one extra for loop , means inside will 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 it be O(n^2) or O(n^2)+O(4)??
It's O(N2). Since 4 is a constant that is independent of N, it does not change the result of big-O.
Of course the third nested loop does make the program run slower. However, since the slowdown is by a constant factor, the asymptotic timing of the program expressed in big-O notation does not change.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With