问题
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