Just a quick preparation for my exam, for example I have:
f(x) = 5x<sup>2</sup> + 4x * log(x) + 2
Would the big O be O(x<sup>2</sup> + x * log(x))
or should I take consideration of non-logarithmic coefficients such as 5 or 4?
Likewise, consider this code
for (int i = 0; i < block.length; i++)
for (int j = 0; j < block.length; j++)
for (int k = 0; k < 5; k++)
g(); //assume worst case time performance of g() is O(1)
So would the big O be O(5n2) or O(n2)?
Complexity for
f(x) = 5x^2 + 4xlogx + 2
isO(x^2)
becauseSo if you have a sum you just select the largest complexity as at the end of the day, when n goes to infinity the largest component will give the most computational load. It also doesn't matter if you have any coeffs because you try to roughly estimate what's going to happen.
For your other sample see reasoning below
First convert loops into sums and work out the sums from right to left
Counter of O(1) from 1 to 5 is still O(1), remember you disregard coeffs
So you end up with the middle sum, which counts a function of O(1) n times, this gives the complexity of O(n)
Finally you get to the leftmost sum and notice that you count n n-times, i.e.
n+n+n...
, which is equal ton*n
orn^2
So the final answer is O(n^2).
O(x**2)
because:lim n^2 if (x->8) = 8
lim 5n^2 if (x->8) = 8
8 is infinity
but if you have the sum of few expressions you need to understood the fastest growing function. it will be an answer on you question.
any other constant before expression will give you the same answer. In that case you should use asymptotic analysis I may give you an advice. If you faced the sum of few function you need:
this element will give you answer