Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linear complexity and quadratic complexity

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?

like image 370
jasonline Avatar asked Apr 14 '26 17:04

jasonline


1 Answers

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

like image 169
jk. Avatar answered Apr 20 '26 10:04

jk.