How is O (N) algorithm also an O (N ^ 2) algorithm?

I read about Big-O notation

Thus, any algorithm that is O (N) is also O (N ^ 2).

It seems to me that I don’t understand, I know that Big-O gives only the upper bound.

But as an O (N) algorithm, it is also an O (N ^ 2) algorithm.

Are there any examples where this is so?

I can’t think of anything.

Can anyone explain this to me?

+4
source share
5 answers

"Upper bound" means that the algorithm takes no more (i.e. <=) that long (since the size of the input tends to infinity, taking into account the corresponding constant factors).

, - .

, O (n) O (n log n), O (n 2), O (n 3), O (2 n), , n.

, .

+3

O "".

, x < 4, , , x < 5 x < 6 ..

O (n) , n (n - , ), " n ".

, x :

x < k * n + C, K C -

, , n, k * n + C.

O (n ^ 2) , , kn ^ 2 + C. n - n ^ 2 >= n, . , x < kn + C, x < k * n ^ 2 + C.

, O (n) O (n ^ 2) O (N ^ 3) O (n ^ n) ..

+3

big-O:

f(x) O(g(x)) iff |f(x)| <= M|g(x)| x >= x0.

, g1(x) <= g2(x), |f(x)| <= M|g1(x)| <= M|g2(x)|.

+1

, - O (N), , N f (N) = k * N k. k * N ^ 2. , O (N) O (N ^ 2) , , O (N ^ m) m > 1.

* , N >= 1, N.

+1

Big-O , , O (n) O (n ^ 2). O (n) O (n ^ 2) . , , . , O (n) alghoritm - O (n ^ 2) alghoritm, .

0

Source: https://habr.com/ru/post/1678450/


All Articles