This is O(n)
:
The outer loop has O(logn)
iterations, since it i
starts with n
and decreases by half by each iteration.
Now consider the number of iterations of the inner loop:
- In the first iteration of the outer loop, the inner loop has
n
iterations (starting at i==n
). n
/2 ( i==n/2
).
...
log(n)
'th ( ) 1 ( i==1
).
, :
n + n/2 + n/4 + ... + 1 <= 2*n = O(n)