I am aware intuitively that two for loops make an O(n^2) function, but what if the loops are unrelated. How is it expressed
For example:
for(x = 1; x < t; x++)
for(y = 1; y < z; y++)
do something trivial
end
end
is the big-o of this O(t*z)? or is it O(n^2) or is it O(t^2). I have always overlooked this, but I would like to know now.
Thanks
It's O(t*z). If you have two nested loop each doing n iterations you have n^2 because of n*n :)
It's like computing the area.. for every t you iterate z times.. so it's intuitively t*z..
Or you can imagine to have a counter inside the loops.. how much will be the result?
for(x = 1; x < t; x++)
for(y = 1; y < z; y++)
do something trivial
end
end
As written, these loops execute (t-1)*(z-1) = t*z - t - z + 1 times -> O(t*z)
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