Why does this O(n^2) code execute faster than O(n)? [duplicate]

Consider:

  • f1(n) = n2
  • f2(n) = n + 1000

Clearly f1 is O(n2) and f2 is O(n). For a small input (say, n=5), we have f1(n) = 25 but f2(n) > 1000.

Just because one function (or time complexity) is O(n) and another is O(n2) doesn’t mean that the former is smaller for all values of n, just that there is some n beyond which this will be the case.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)