3

The formal definition of the Big O notation is that if we have a function f(n) (for time and space of an algorithm) and another function g(x), and there are constants c and no such that c*g(n) > f(x) for all n > no, then f(n) = O(g(n)). But using this definition and the fact that a growing quadratic function always will surpass a linear function at some point, is it true that all O(n) functions are also O(n²)? Or better stated, is n = O(n²)?

Joe
  • 2,352
  • 20
  • 38
vacih86456
  • 237
  • 1
  • 7

2 Answers2

6

Yes, all O(n) algorithms are O(n²), too. People are pretty sloppy with notation when it comes to Big-O. To be clear, I think it's best to conceptualize O(f) as returning a set of functions. Using set notation:

n ∈ O(n) ⊂ O(n²)
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
3

Certainly. Apply your definition with f(n)=n, g(n)=n^2, c=1, and n0=1, to see that f is O(n^2). All you need to notice is that when n > n0 = 1, we have

n = 1*n = n0*n < n*n = n^2

A similar argument shows that every O(n) function is O(n^2).

In essence, big O is about providing an asymptotic upper bound. There is no requirement that this upper bound be sharp; that is what big Theta is for.

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82