在哪里可以找到 O(n^2) 和 O(n) 等的含义?
我一直在注意到有关堆栈溢出的答案使用此类术语,但我不知道它们的含义。它们叫什么?有没有好的资源可以简单地解释它们?
I've been noticing answers on stack overflow that use terms like these, but I don't know what they mean. What are they called and is there a good resource that can explain them in simple terms?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该表示法称为 Big O 表示法,并用作表达算法复杂性的简写(基本上是随着输入大小 (n) 的增长,给定算法需要运行多长时间)
一般来说,您将遇到以下主要类型的算法:
一般来说,您可以通过查看算法的使用方式来粗略地衡量算法的复杂性。例如,查看以下方法:
这里必须完成一些操作:
这里有一些操作在恒定时间内运行(前两个和最后一个),因为 x 的大小不会影响它们运行的时间。此外,还有一些操作以线性时间运行(因为它们针对 x 中的每个条目运行一次)。使用 Big O 表示法,算法被简化为最复杂的算法,因此该求和算法的运行时间为 O(n)
That notation is called Big O notation, and is used as a shorthand to express algorithmic complexity (basically how long a given algorithim will take to run as the input size (n) grows)
Generally speaking, you'll run into the following major types of algorithims:
Generally you can get a rough gauge of the complexity of an algorithim by looking at how it's used. For example, looking at the following method:
There's a few things that have to be done here:
There's a few operations that run in constant time here (the first two and the last), since the size of x wouldn't affect how long they take to run. As well, there are some operations that run in linear time (since they are run once for each entry in x). With Big O notation, the algorithim is simplified to the most complex, so this sum algorithim would run in O(n)
首先阅读计算复杂性,然后尝试一些有关算法的书籍,例如算法简介。
来自维基百科页面:
如果您不愿意深入研究细节,您通常可以通过分析其代码来近似算法复杂性:
有时,估计函数/算法大 O 表示法复杂性并不那么简单在这种情况下,使用摊销分析。上面的代码只能作为快速入门。
Read about Computational Complexity first, then try some books about algorithms like Introduction to Algorithms.
From Wikipedia page:
If you don't won't to drill into details you can very often approximate algorithm complexity by analizing its code:
Sometimes it is not so simple to estimate function/algorithm big O notation complexity in such cases amortized analysis is used. Above code should serve only as quickstarter.
这称为Big O 表示法,用于量化算法的复杂性。
O(1) 意味着无论要处理多少数据,算法都需要恒定的时间。
O(n)表示算法速度随着数据量的增加呈线性增长。
等等...
因此,O 表示法中 n 的幂越低,算法解决问题的效果就越好。最好的情况是 O(1) (n=0)。但许多问题都有其固有的复杂性,因此几乎在所有情况下您都找不到如此理想的算法。
This is called the Big O notation and is used to quantify the complexity of algorithms.
O(1) means the algorithm takes a constant time no matter how much data there is to process.
O(n) means the algorithm speed grows in a linear way with the amount of data.
and so on...
So the lower the power of n in the O notation the better your algorithm is to solve the problem. Best case is O(1) (n=0). But there is an inherent complexity in many problems so that you won't find such an ideal algorithm in nearly all cases.
到目前为止,答案都很好。网络搜索的主要术语是“Big O notation”。
“someformula is O(someterm)”数学背后的基本思想是,当变量趋向无穷大时,“someterm”是公式中占主导地位的部分。
例如,假设您有
0.05*x^3 + 300*x^2 + 200000000*x + 10
。对于非常小的 x 尺寸(x==1 或 x==2),200000000*x
将是迄今为止最大的部分。此时,公式图将看起来呈线性。随着您的继续,在某些时候300*x^2
部分会更大。但是,如果您继续使 x 变得更大,无论您想要多大,0.05*x^3
部分将是最大的,并且最终将完全超过公式的其他部分。从图中可以清楚地看出您正在查看一个立方函数。所以我们可以说公式是O(x^3)
。The answers are good so far. The main term to web search on is "Big O notation".
The basic idea behind the math of "someformula is O(someterm)" is that, as your variable goes to infinity, "someterm" is the part of the formula that dominates.
For example, assume you have
0.05*x^3 + 300*x^2 + 200000000*x + 10
. For very low sizes of x (x==1 or x==2), that200000000*x
will be by far the biggest part. At that point a plot of the formula will look linear. As you go along, at some point the300*x^2
part will be bigger. However, if you keep making x even bigger, as big as you care to, the0.05*x^3
part will be the largest, and will eventually totally outstrip the other parts of the formula. That is where it becomes clear from a graph that you are looking at a cubed function. So we would say that the formula isO(x^3)
.