Why is the Big-O complexity of this algorithm O(n^2)?

前端 未结 7 2040
天命终不由人
天命终不由人 2021-02-03 17:03

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--) 
  s         


        
7条回答
  •  执念已碎
    2021-02-03 17:50

    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).

提交回复
热议问题