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

前端 未结 7 2045
天命终不由人
天命终不由人 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 18:05

    Let m be the number of iterations taken. Then,

    i+m = n^2 - m

    which gives,

    m = (n^2-i)/2

    In Big-O notation, this implies a complexity of O(n^2).

    0 讨论(0)
提交回复
热议问题