如何计算复杂度?
我是算法初学者,不知道如何计算复杂度。
Example:
int x=10,y;
y = x;
上面例子的复杂度是多少?
谢谢
I am a beginner in algorithms and I don't know how to calculate complexity.
Example:
int x=10,y;
y = x;
What is the complexity in example above?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您参考 O 表示法,那应该是 O(1)。
That should be O(1) if you refer to the O-Notation.
在 Big O Notation 中,这对应于
O(1)
基本上意味着操作的运行时间是恒定的或至少小于某个常数。因此,运行时间不依赖于您的输入。正如您可能从我所写的内容中推断出的那样,大 O 表示法仅给出了操作的上限。还有其他给出下限的符号等等。它确实取决于输入的情况的示例可能是:
这里运行时间取决于数组有多大,如果我们将数组的长度指定为变量
n
,那么这将为O(n)
。同样,大 O 表示法并没有具体指定执行需要多长时间,但在本例中,只是表示我们可以将n
乘以某个常量,然后它将在内完成n*一些
s。这里给出了更详细的解释:什么是“Big O”符号的简单英语解释?
In the Big O Notation this corresponds to
O(1)
which basically means that the run-time of the operation is constant or at least is less than a certain constant. Ergo, the run-time does not depend on the input you have. As you may infer from what I wrote, the Big O Notation only gives an upper bound of the operation. There is also other notations which give a lower-bound and so on.An example of a case where it does depend on the input could be:
Here the run-time depends on how big the array is, and if we give the length of the array the variable
n
then this will beO(n)
. Again, the Big O Notation does not specify exactly how long it will take to execute but, in this case, just says that we can multiplyn
by some constant and then it will be finished withinn*some
s.A more detailed explanation is given here: What is a plain English explanation of "Big O" notation?