Linear complexity and quadratic complexity

前端 未结 4 1043
攒了一身酷
攒了一身酷 2021-01-14 20:16

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) i
4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-14 21:13

    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

提交回复
热议问题