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

前端 未结 7 2042
天命终不由人
天命终不由人 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:55

    Your algorithm is equivalent to

    while (i += 2 < n*n) 
      ...
    

    which is O(n^2/2) which is the same to O(n^2) because big O complexity does not care about constants.

提交回复
热议问题