Why is the Big-O complexity of this algorithm O(n^2)?
I know the big-O complexity of this algorithm is O(n^2)
, but I cannot understand why.
int sum = 0;
int i = 1; j = n * n;
while (i++ < j--)
sum++;
Even though we set j = n * n
at the beginning, we increment i and decrement j during each iteration, so shouldn't the resulting number of iterations be a lot less than n*n
?
Solution 1:
During every iteration you increment i
and decrement j
which is equivalent to just incrementing i
by 2. Therefore, total number of iterations is n^2 / 2 and that is still O(n^2).
Solution 2:
big-O complexity ignores coefficients. For example: O(n)
, O(2n)
, and O(1000n)
are all the same O(n)
running time. Likewise, O(n^2)
and O(0.5n^2)
are both O(n^2)
running time.
In your situation, you're essentially incrementing your loop counter by 2 each time through your loop (since j--
has the same effect as i++
). So your running time is O(0.5n^2)
, but that's the same as O(n^2)
when you remove the coefficient.