BigO runtime for some methods

Well, all of these are fairly simple methods, and there are several of them, so I did not want to just create some questions when they are all the same. BigO is my weakness. I just can't understand how they came up with these answers. In any case, can you give me some idea of ​​your thinking for analyzing the runtime of some of these methods? How do you break it? How should I think when I see something like this? (in particular, the second, I do not understand how it is O (1)) alt text

+5
source share
4 answers
function f1:
  loop 3 times
    loop n times

Therefore, O (3 * n), which is effective O (n).


function f2:
  loop 50 times

O (50) is effective O (1).

, 50 , , n = n - (n / 50) 0. , 50 (n - (n / 50)*50 = 0).


function f3:
  loop n times
    loop n times

O (n ^ 2).


function f4:
  recurse n times

, , n = - + 1. +1. , n = - .

,

arr[hi] * arr[low] > 10

, , , ().

, n = - 0, n .


function 5:
  loops ceil(log_2(n)) times

- m/=2.

, n = 10. log_2 (10) = 3,3, 4.

10 / 2 = 
  5 / 2 = 
   2.5 / 2 = 
    1.25 / 2 = 
      0.75

4 .

+6

n ^ 2 , , . n ^ 2, . 3n, Big-O.

, , , , , - O (1). , , , , O (n) . .

O (n), lo + 1. , for lo ++/++ lo.

O (log n), . , , , .

. , , . O (n), , , , .

+4

Big-O : " N , , ?" , ( ) "5 * N + 35", "N". . , , , N . , O (N), O (N * N), O (logN) O (N!), , "" , . O (N) , O (N * N), N , , .

, : , N. - , , . , , N M ( - - O (N * M * M)).

, N, , N. , .

, , , , N. (1), O (N), O (N * N), O (logN), O (N!) , , N. O (N!) , , .

- N * N + N + 1, , , , N , . , . O (5 * N) - , O (N), , .

: , :

. , - O (N), - . O (N). ( - O (3N) = O (N)).

. . - , , 50 . N, O (1). , , , 10 , , . O (∞), . , ? ...

, O . . , , -O , , - , . .

... , " ". "" , Big-O, . . , Quicksort. O (N * logN), O (N * N). , , .

+4

The second is 50, because the big O is a function of the input length. That is, if the input size varies from 1 million to 1 billion, the execution time should increase by 1000 if the function is O (N) and 1 million if it is O (n ^ 2). However, the second function is performed in time 50 regardless of the input length, therefore, O (1). Technically it will be O (50), but the constants do not matter for large O.

+1
source

All Articles