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²)
?
Asked
Active
Viewed 239 times
3

Joe
- 2,352
- 20
- 38

vacih86456
- 237
- 1
- 7
-
2O(n) functions are also O(n²) is true if `0*x^2 + B*x + C` is a [Quadratic](https://en.wikipedia.org/wiki/Quadratic_equation), but that is usualy considered _linear_. – chux - Reinstate Monica Sep 02 '21 at 23:34
-
5Indeed, that's why we have big-Theta notation. – David Eisenstat Sep 02 '21 at 23:46
2 Answers
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