What is the time complexity of the code below?
1 2 3 4 5 6 |
|
Assume that arithmetic operations take constant time regardless of the size of the input.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
In the first iteration, the j loop runs N times.
In the second iteration, the j loop runs N / 2 times.
In the ith iteration, the j loop runs N / 2 i times.
So, the total number of runs of loop = N + N / 2 + N / 4 + … 1 = N ∗ ( 1 + 1 / 2 + 1 / 4 + 1 / 8 + … ) < 2 ∗ N