Worst case time complexity analysis pseudocode

后端 未结 1 1273
鱼传尺愫
鱼传尺愫 2021-01-26 15:39

Could someone help me out with the time complexity analysis on this pseudocode? I\'m looking for the worst-case complexity here, and I can\'t figure out if it\'s O(n^4), O(n^5)

相关标签:
1条回答
  • 2021-01-26 16:08

    First loop: O(n)

    Second loop: i is in average n/2, you could have an exact formula but it's O(n²)

    Third loop happens i times inside the second loop, so an average of n/2 times. And it's O(n²) as well, estimating it.

    So it's O(n*n²*(1 + 1/n*n²)), I'd say O(n^4). The 1/n comes from the fact that the third loop happens roughly 1/n times inside the second one.

    It's all a ballpark estimation, with no rigorous proof, but it should be right. You could confirm it by running code yourself.

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