Why is O(n) equal to O(2n)

空扰寡人 提交于 2021-02-07 14:49:58

问题


I understand that O(N) is essentially equal to O(cN) where c='some constant'. But if N = c. Doesn't that make it O(N)^2. Does this hold as c increases, or is there some formal limit.


回答1:


If N = c then c is not constant. Therefore this is never the case.




回答2:


O(n) means that the runtime of the algorithm increases linearly with the size of the input. If you double the size of the input, you double the runtime. If you triple the size of the input, you triple the runtime. And so on. So the graph is a straight line.

O(n^2) means that the runtime of the algorithm increases quadratically with the size of the input. If you double the size of the input, you quadruple the runtime. This is bad. The graph kind of loops up and gets really high really fast.

With O(2n), you have increased the slope of the line, but it is still a line. Since it's linear, it "reduces" to O(n).

Hope that helps.



来源:https://stackoverflow.com/questions/19371489/why-is-on-equal-to-o2n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!