Order Of Growth complicated for loops

后端 未结 2 840
抹茶落季
抹茶落季 2021-01-16 12:47

For the following code fragment, what is the order of growth in terms of N?

int sum = 0;
for (int i = 1; i <= N; i = i*2)
  for (int j = 1; j <= N; j =         


        
2条回答
  •  滥情空心
    2021-01-16 12:55

    To my understanding, the outer loop will take log N steps, the next loop will also take log N steps, and the innermost loop will take at most N steps (although this is a very rough bound). In total, the loop has a runtime complexity of at most ((log N)^2)*N, which can probably be improved.

提交回复
热议问题