简体   繁体   English

简单两种情况的算法分析

[英]Algorithm Analysis of Simple 2 Cases

Well I am studying algorithms for an upcoming exam and I am having hard time verifying if the practices below are answered right. 好吧,我正在研究即将举行的考试的算法,并且很难验证下面的做法是否正确。

 i=1
 while(i<=n)
      j=1
      while(j<i)
            j=j+1
      i=i*2

The answer of mine for this one is O(n.log n) 我的答案是O(n.log n)

i=1
 while(i<=n)
      j=1
      while(j<i)
            j=j*2
      i=i+1

Again I have answered it as O(n.log n) 再次我回答为O(n.log n)

Can someone verify that I am answering correct or not ? 有人可以验证我回答的正确与否吗? Also any tips regarding analysis for future practices are welcome. 也欢迎任何有关未来实践分析的技巧。

Yes, each of these is O(n log n) . 是的,每个都是O(n log n) You have one loop that is a linear iteration, ie O(n) . 您有一个循环是线性迭代,即O(n) You have another loop that is exponential; 您还有另一个指数循环。 the inverse is log, so that loop is O(log n) . 逆是log,因此循环是O(log n) As they are nested, you multiple the complexities, and get O(n log n) . 由于它们是嵌套的,因此您需要提高复杂性,并获得O(n log n)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM