What are the differences between O (1) and O (2) in the analysis algorithm?

In accordance with the definition of large O f(n) <= C*g(n)(which means f(n) = O(g(n)), we could conclude that:

f(n) <= C
f(n) <= 2C

I think there are no big differences between the two. I could come up with the following:

f(n) = 1 - 1 / n
f(n) = 2 - 1 / n
C = 1

But what is the difference between these two difficulties, since both are constant difficulties?

Could you show the real world code to demonstrate the differences between O (1) and O (2).

+5
source share
8 answers

O(1) O(2). , O(1), O(2) . , O(c1) O(c2) c1 c2.

O(c) c - , , . (), O(1) O(2) .

f O(1). c , f(n) <= c * 1 n. d = c / 2. f(n) <= c = (c / 2) * 2 = d * 2, , f O(2). , g O(2), c , g(n) <= c * 2 n. d = 2 * c. g(n) <= c * 2 = d = d * 1, , g O(1). O(1) = O(2).

+17

O (1) O (2) , O ( ).

, N.

+9

.

O (n), O (n 2).

http://img685.imageshack.us/img685/9715/graph.gif

, 2 1 , x ( ). , Big-O- ; .

+3

, , ( N), . .

+2

O (1) O (2).

. O (f (x)) , k , k f ( x).

- O (2), k, 2 k. , k '= 2 k, O (1).

0

O (1) O (2) . . O (N) O (n ^ 2), O (log (N)) .. . , O (1) . O (N) (N), O (n ^ 2) ..

0

Big-O , .. , n . n f (n).

, n big-O (, f (n) = 2n ^ 2 + 4 O (n ^ 2) big-O ).

In the case where the leading term is constant and independent of n, then all these constants are virtually equally asymptotically and usually simply reduce to O (1).

Therefore, O (2) will be considered equivalent to O (1).

0
source

Usually you do not write O (2) or O (2n), but O (1) and O (n). Of course, the difference in real speed is 5 s versus 10 s. I found your question a bit confusing.

-1
source

All Articles