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)
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.