Great notation for the algorithm

I am busy with a task and I am struggling with a question. I know that I should not ask questions about the appointment directly, so I understand if I do not get direct answers. But it doesn’t matter here.

We have to calculate the runtime complexity for different algorithms, I'm stuck with this.

for(int i = 1 ; i < n ; i++)
    for(int j = 0 ; j < i ; j +=2)
        sum++;

Now, with my understanding, my first thought will be less than O (n 2 ), because the nested loop does not work full n times, and yet the variable j is incremented by 2 every loop, rather than iterating like a regular loop. Although, when I did some modeling of the code with N = 10, N = 100, N = 1000, etc., I got the following results when I deduced the sum variable.

N = 10 : 25, 
N = 100 : 2500,
N = 1000 : 250000,
N = 10000 : 25000000

When I look at these results, O Notations seems to have much more than just O (n).

4 , : O (1), O (n 2), O (n) O (logn). , , , O (n 2), . , , .

!

+4
6

Big O . , . , .

c c^2.

() , (n^2)/4.

, , , , , "" . n^2. "i" "j". , start 1 end N, N*N ( ).

i < j. , 1- 0.5, , 0.5; , 1/4.

O(0.25 * n^2) = O(n^2). , . N.

+11

, big-O - . ( ) .

, n , i - i / 2 . / 2, 1 .. n, n * (n + 1) / 2. a * n^2 + b * n + c a, O(n^2).

n n / 2. - (n/2) * ((n/2) + 1) / 2. d * n^2 + e * n + f d, O(n^2).

+2

: sum ~ = (n ^ 2)/4.

, , O (n ^ 2) ( O ). Big-O. . http://en.wikipedia.org/wiki/Big_O_notation.

+1

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

+1

for (int i = 1 ; i < n ; i++)
    for (int j = 0 ; j < i ; j +=2)
        sum++;

:

0+2+4+6+...+2N == 2 * (0+1+2+3+...+N) == 2 * (N * (N+1) / 2) == N * (N+1)

n == 2N (n / 2) * (n / 2 + 1) ~ = (n * n) / 4

so O(n²)

0

. - sum.'sum ' , , .

:

 for(int i = 1 ; i < n ; i++)
    for(int j = 0 ; j < i ; j +=2)
        sum++;

( n). . . , Big O . Bog O .

(n-1) . (n-1) ( = 1) i/2 . = 1 + 1 + 2 + 2 + 3 + 3 +... + (N/2) + (N/2) = 2 (1 + 2 + 3 +... + /2) = 2 * (/2 (N/2 + 1))/2 = ^ 2/4 + /2. "sum ++" n ^ 2/4 + n/2 . 1 = c1, 2 = c2 3 = c3. . , = c1 * (n-1) + c2 * (n ^ 2/4 + n/2) + c3 * (n ^ 2/4 + n/2) = (c2 + c3) n ^ 2/4 + (c2 + c3) n/2 + c1 * n-c1. , . Big O , O ((c2 + c3) n ^ 2/4 + (c2 + c3) n/2 + c1 * n-c1). Big O . n n ^ 2 n. , O ((c1 + c2) n ^ 2/4). , n, n ^ 2 (c1 + c2) n ^ 2/4 , , O (n ^ 2).

0
source

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


All Articles