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

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

    You will have exactly n*n/2 loop iterations (or (n*n-1)/2 if n is odd). In the big O notation we have O((n*n-1)/2) = O(n*n/2) = O(n*n) because constant factors "don't count".

提交回复
热议问题