繁体   English   中英

这个循环的时间复杂度是多少

[英]What is the time complexity for this loop

我们怎样才能找到这个循环的时间复杂度

int c = 0;
int j = 1;
while (j< n^3) {
  c+=1;
  System.out.println(c);
  j=j*4;
}

由于每次 j 乘以 4 我们可以说在每次迭代之后它可以写成:

1, 4, (4^2), ..., (4^k)

现在 for 循环为假, (4^k) >= n^3

4^k >= n^3
k = log(n^3) to the base 4

您可以将其进一步简化为:

3log(n) to base 4并删除 3,就像我们对常量所做的那样。

k = log(n)

这应该是你的循环的复杂性。

暂无
暂无

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

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