Linear complexity and quadratic complexity

≯℡__Kan透↙ 提交于 2020-01-11 06:28:06

问题


I'm just not sure...

If you have a code that can be executed in either of the following complexities:

  1. A sequence of O(n), like for example: two O(n) in sequence
  2. O(n²)

The preferred version would be the one that can be executed in linear time. Would there be a time such that the sequence of O(n) would be too much and that O(n²) would be preferred? In other words, is the statement C x O(n) < O(n²) always true for any constant C?

Why or why not? What are the factors that would affect the condition such that it would be better to choose the O(n²) complexity?


回答1:


I think there are two issues here; first what the notation says, second what you would actually measure on real programs

  1. big O is defiend as a limit as n -> infinity so in terms of big O, O(n) < O(n^2) is always true regardless of any finite constants.

  2. as others have pointed out real programs only ever deal with some finite input, so it is quite possible to pick a small enough value for n such that the c*n > n^2 i.e. c > n, however you are strictly speaking no longer dealing with big O




回答2:


If your constant C is greater than your value of n, then the O(n²) algorithm would be better.




回答3:


There is always an implied constant in O notation, so yes, it's possible that for sufficiently small n that O(n^2) may be faster than O(n). This would happen if the constant for O(n) was much smaller than that for O(n^2).




回答4:


C x O(n) < O(n²) is NOT always true, there is a point in n where it reverses the condition.

When C is large and n is small, then C x O(n) > O(n²). However, C is always constant, hence when n scales to a large number, C x O(n) < O(n²).

Therefore, when n is large, O(n) is always better than O(n²).



来源:https://stackoverflow.com/questions/2771854/linear-complexity-and-quadratic-complexity

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