Complexity for nested loops dividing by 2

后端 未结 3 1460
感动是毒
感动是毒 2020-12-29 14:19

I am trying to figure out the complexity of a for loop using Big O notation. I have done this before in my other classes, but this one is more rigorous than the others becau

3条回答
  •  长发绾君心
    2020-12-29 14:35

    The overall number of iterations of inner loop is n + n/2 + n/4 + ... + 1 which is approximately 2n. So the complexity is O(n).

提交回复
热议问题