Order of magnitude using Big-O notation

This is probably the soil that has been covered, but I have yet to find an explanation that I can understand. It is likely that I will soon feel embarrassed.

For example, I'm trying to find the order of magnitude using a Big-O notation like this:

count = 0;
for (i = 1; i <= N; i++)
    count++;

Where can I find what determines magnitude? I am relatively poor in mathematics, and although I tried several resources, I have not yet found one that can explain how part of the code translates into an algebraic equation. Honestly, I can’t even guess that the effectiveness of Big-O concerns this cycle.

+4
source share
2 answers

( O, , ) , "" ( ) , .

O, : f (x) g (x), f (x) = O (g (x)), , x, g (x ) f (x). "", (, f (x) > g (x) x), g (x) (g (x) >= f (x)). , ( ). Big-O , ( , ).

"" . , : - ? ?

, N ( ), O (N). N = 10 10 , N = 100 = > 100 , N = 1000 = > 1000 ... , .

:

for (int i = 0; i < N; i++) {
   if (i == randomNumber()) {
      // do something...
   }
}

, , , , " -" . , , , , , big-O ( ), , O (N).

:

for (int i = 0; i < N; i++) {
   for (int i = 0; i < N; i++) {
       // do something
   }
}

, N , . N = 10, 10x10, N = 100 = > 100x100 , N = 1000 = > 1000x1000 . , , N x N, O (N x N).

. , , . , , ( ), , N ? :

Node actual = root;
while(actual.left != null) {
   actual = actual.left
}
// in actual we have left-most leaf

( ) ? , , ? ? - log (N) - = 2. , O (log (N)) - , , , (, , ...)

+2

-

O (N)

N=number of elements, , ,

for (int i=0; i < N; i++) {
    // some process performed N times
}

" ", , , ; O (N) , , , . , , O (N) " ", N . "-" , , . "-O" , , N . "" "" , , , , , , , N " N" )

,

for (int i=0; i < N; i++) {
   for (int j=0; j < N; j++) {
    // process here NxN times
   }
}

O (N 2)

, N = 10, "" 10 , 10x10 = 100 (= ). , , N , , , , . , - O (N), O (N 2), x386, ,

+2

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


All Articles