算法速度顺序
有时我会完全被愚弄,尝试用 O(x) 表示法来估计算法的速度,我的意思是,我真的可以指出顺序何时为 O(n) 或 O(mxn),但对于那些 O(lg( n)) 或 O(C(n)) 我认为我错过了一些东西...... 那么,您有哪些技巧和窍门可以轻松估计并快速忽略算法呢?
作为我正在寻找的示例,这里有一些简单的循环(可能是错误的,但尽我所能):
- O(n): 如果有一个从 1 到 n 的简单循环(或其中几个,但是不嵌套。
- O(mxn):另一个嵌套循环,其限制是 m 和 n
。
Sometimes I get totally fooled trying to estimate an algorithm's speed with the O(x) notation, I mean, I can really point out when the order is O(n) or O(mxn), but for those that are O(lg(n)) or O(C(power n)) I think that I am missing something there...
So, what are your tips and tricks for an easy estimate with a fast overlook on an algorithm?
As an example of what I am looking for, here are some for the easy ones (can be wrong, but trying my best):
- O(n): If there is a simple loop from 1 to n (or several of them, but not nested.
- O(mxn): A nested loop inside of another where the limits are m and n.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
递归、分治算法通常是 O(logN)。 循环分而治之的算法将是 O(NlogN)。
Recursive, divide-and-conquer algorithms are often O(logN). An algorithm that loops over a divide-and-conquer would be O(NlogN).
这是一篇可能有帮助的博客文章:
将事物分解并重新组合起来的成本
那篇文章解释了处理大 O 订单的“主定理”。
Here's a blog post that might help:
The cost of breaking things down and putting them back together
That post explains the "master theorem" for working with big-O orders.
O(lg(n)):如果您的问题在算法的每个步骤中按 n(通常为 n/2)的某个比例变小,并且每个步骤执行恒定量的工作。 二分搜索是一个很好的例子,因为每一步都通过执行恒定的工作量(计算中点并进行一次比较)来将问题大小减少一半。
请注意,n 位于该比例的顶部。 这与每一步减少 1/n 的问题大小不同。 :)
O(lg(n)): If your problem gets smaller by some proportion of n (often n/2) at each step of your algorithm and each step does a constant amount of work. Binary search is a good example since each step cuts your problem size in half by doing a constant amount of work (calculate the mid-point and do one comparison).
Note that the n is on the top of that proportion. This is not the same as your problem size reducing by 1/n at each step. :)
如果您正在寻找一种快速方法来估计算法的运行时间,那么其他答案都很好。 如果您想要更详细的答案,我建议您查看“主定理”。 在德语文章中,有一个很好的表格。
编辑:约翰·D·库克对主定理做了很好的回顾,请参阅链接他的答案。
If you're looking for a quick way to estimate the runtime of an algorithm, the other answers are good. If you want a more elaborate answer, I suggest you look at the "Master theorem". In the German article, there is a nice table to that.
Edit: John D. Cook has made a nice recap to the master theorem, see the Link his answer.
关于 Big O 表示法的维基百科文章有一个很好的常用函数的顺序。
The Wikipedia article on Big O Notation has a nice chart of the orders of common functions.
算法的渐近复杂性在实践中很重要,以下是我在审查我的或其他人的代码时使用的一些经验法则。 通常实用的计算机算法是许多变量和重要数据结构的函数,但让我们假设(仅用于说明)我们的算法 f 基本上采用单个整数 X 作为其参数,并且我们希望根据以下公式找到 f 的渐近复杂度: X. 假设 f(0) 是微不足道的。 那么一般来说:
一些特殊情况:
通过追加字符串或内存区域来增长字符串或内存区域的算法,在许多语言中都会产生 O(X**2) 开销,例如
这个典型的嵌套循环也有 X**2 开销:
C++ STL 容器(如 std::set 和 std::map)几乎所有操作都有 O(log X) 开销
只需我的 2 美分! 希望这可以帮助!
the asymptotic complexity of algorithms is important in practice, and here are some of rules of thumb I use when I review mine or other people's code. Usually practical computer algorithms are functions of many variables and nontrivial data structures, but let us assume (just for illustration) that our algorithm f takes basically a single integer X as its argument, and we want to find the asymptotic complexity of f in terms of X. Suppose f(0) is trivial. Then in general:
Some special cases:
Algorithms which grow strings or memory areas by appending to them, do in many languages incur O(X**2) overhead, such as
This typical nested loop has also X**2 overhead:
C++ STL containers like std::set and std::map have O(log X) overheads for almost all operations
Just my 2 cents! Hope this helps!
通常会出现类似 O(logN) 的情况,因为数据大小为 N,但它是组织在树中,其中树的深度为 logN。 如果典型的搜索涉及从根到叶(在最坏的情况下),那么很容易看出该算法将是 O(logN)。
没有硬性规定——你只需查看每种情况,找出最坏的情况,并计算出这种情况的成本。
Usually something like O(logN) comes about because the data is size N, but it is organized e.g. in a tree where the depth of the tree is logN. If the typical search involves going from the root to the leaf (in worse case) then it is easy to see that the algorithm will be O(logN).
There are no hard and fast rules - you just have to look at each case, figure out the worse case scenario, and calculate what the cost of that would be.
我不太喜欢回答自己的问题,但我今天发现了这个并提醒我这个问题。
http://bigocheatsheet.com/
I am not a big fan of answering my own questions, but I found this today and remind me this SO question.
http://bigocheatsheet.com/