Time complexity analysis. choosing operator for counting number of times a line of code runs

自古美人都是妖i 提交于 2019-12-02 05:22:45

If your while loop is:

3 while i < n                 log n + 1
4     for j = 1 to n           n log n
5        sum = sum + j         n log n
6     i = 2i                   log n

Then yes, you are correct in calculating the complexity. The complexity of the code is indeedO(nlogn).

Edit:
Though I am curious what you are trying to do here. You are calculating the sum of 1st n elements logn times.

Hence the return value would be something like n*(n+1)/2 * logn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!