想知道算法的时间复杂度
在我的工作中,我必须实现一个算法,具体细节并不重要,但我无法对这个特定算法的时间复杂度有一个明确的答案。
简而言之,它看起来像这样:
for(let i = 0; i < 3; i++) {
for(let j = 0; j < n - 1; j++) {
for(let k = 0; k < 8; k++ {
// Do some stuff
}
}
}
这个算法的时间复杂度是多少?
一开始我以为会是 O(n^3) 之类的。但我想得越多,我就越认为它实际上更像是一个 0(n)。因为,尽管我们有 3 个 for 循环,但只有一个可以是可变大小的,另外两个是恒定的。
那么这个算法的实际时间复杂度是多少呢?是 O(n^3)、O(n) 还是其他?
In my job I had to implement an algorithm, the specifics details does not matters, but I was unable to have a clear answer about the time complexity of this specific algorithm.
In a nutshell it's look like something like that:
for(let i = 0; i < 3; i++) {
for(let j = 0; j < n - 1; j++) {
for(let k = 0; k < 8; k++ {
// Do some stuff
}
}
}
What is the time complexity of this algorithm ?
At the beginning I thought it would be something like O(n^3). But the more I think about, the more I think it's actually more a 0(n). Because, even though we have 3 for loop, only one can be of a variable size, the two other are constant.
So what is the actual time complexity of this algorithm ? Is it O(n^3), O(n) or even something else ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,时间复杂度为
O(n)
。你是对的。总执行次数为
3 * n * 8 = 24*n
,其中对于较大的n
可以忽略 24。所以复杂度是O(n)
Yes, it would be
O(n)
. You are right.The total number of execution is
3 * n * 8 = 24*n
where 24 can be ignored for largen
. So the complexity isO(n)
直接答案
除了已经给出的很好的答案之外,我希望您能够描绘每个循环的计算:
某些隐式规则与:
O(1)
示例:
3 = O(1)
、8 = O (1)
示例:
O(n-1) = O(n) -1 = O(n)
kx O(n) = O(kn) = O(n)
示例:<代码>3x O(n) = O(3n) = O(n)
扩展
让我们改变你的循环,让你有
O(n^3)
正如你想知道的那样:因为每个循环都有复杂度为
O(n)
,我们有O(n) x O(n) x O(n) = O(n^3)
Direct answer
Besides great answers already given, I'd like you to picture the calculation over each loop:
Certain implicit rules applied are relative to:
O(1)
example:
3 = O(1)
,8 = O(1)
O(n + k) = O(n) + k = O(n)
example:
O(n-1) = O(n) -1 = O(n)
k x O(n) = O(kn) = O(n)
example:
3x O(n) = O(3n) = O(n)
Extension
Let's change your loop to have case where you'd have
O(n^3)
as you were wondering:Because each loop has a complexity of
O(n)
, we haveO(n) x O(n) x O(n) = O(n^3)
代码的时间复杂度为
O(n)
:由于外循环迭代固定次数,因此内循环每次迭代
n - 1
次,并且对于其中的每一次,最内层的循环都会迭代恒定的次数。因此,您得到
3 * (n - 1) * 8 = 24n - 24
,并且由于常量被大 O 表示法丢弃,您得到O(n)
。话虽这么说,代码的时间复杂度可能会受到最内层循环内部运行的代码(即
// Do some stuff
部分)的影响。如果您只在那里执行固定数量的操作,则代码复杂性不会受到影响。但是,例如,如果您在其中对大小为 n 的数组进行排序,那么这会将时间复杂度更改为n * nlogn
。The time complexity of the code is
O(n)
:As the outer loop iterates a constant number of times, for each time its inner loop iterates
n - 1
times, and for each of these times, the innermost loop iterates a constant number of times.Therefore you get
3 * (n - 1) * 8 = 24n - 24
and as constants are discarded for big-O notation, you getO(n)
.That being said, the time complexity of the code can be affected by the code that runs inside of the innermost loop (i.e the
// Do some stuff
part). If you are only doing a constant number of operations there, the code complexity will not be affected. However, if for example, you sort an array of size n in there, then this will change the time complexity ton * nlogn
.此代码的时间复杂度为
O(n)
。外部和内部循环具有恒定的迭代次数,并且与输入大小 (n
) 无关。对于大 O 表示法,常量将被丢弃。Time complexity for this code is
O(n)
. Outer and inner loops have constant number of iterations and are independent from the input size (n
). And constants are discarded for big O notation.时间复杂度将为
O(n)
,因为 3 和 8 是可以忽略的常量。另外,第二个循环将持续到n - 1
,其中 1 也被忽略。注意:当n
的值变得非常大时,就会出现对常量的无知。如果在所有这些循环中调用一个函数,那么对该函数的总调用次数为 n,但精确地说,总调用次数将是
我们在大 O 表示法中忽略的。
在
C
中尝试以下代码:实时演示
< /strong>Time complexity will be
O(n)
, because 3 and 8 are constants which can be ignored. Also, the second loop is going tilln - 1
, where 1 is also ignored. NOTE: All this ignorance of constants is whenn
becomes really large in value.If inside all these loop a function is called, then the total calls to the function is
n
, but in precise manner total call will beWhich we ignore in big O notation.
Try this code in
C
:LIVE DEMO