Greater time complexity for nested loops

I have a question, here is the loop:

for (i=0; i < n; ++i)
   for (j = 3; j < n; ++j)
           {
            ...
           }

I kind of understand how to calculate the big-oh, but I'm not quite sure how to do this. The outer loop executes n times, and the inner loop executes i times for each value of i. The difficulty should be N ^ 2 (I think). Can you guys talk about how this is calculated? I understand some of them, but not all.

+3
source share
6 answers

This (n*(n-3)) = nΒ²-3n, but for the very large nit is close to nΒ². Therefore, to designate Big-Oh, I would write O(nΒ²), because you -3ncan ignore it.


: n , (n-3) .

+9

O (n ^ 2). , "...". O (1), , O (n ^ 2). , n-3 . 1 , O (1). , n * (n-3) . , O (1), O (n * (n-3)) = O (n ^ 2).

+4

, : , . , , , O (mn 2), m - , , ( n, ).

, , , n 2 - 3n O (n 2). , , n 2 - 3n & le; cn 2 c - . = 2 n 2 - 3n & le; n 2 + n 2 , , .

+1

, O (n ^ 2), . - :

for (int x=0; x < width; x++) {
  for (int y=0; y < height; y++) {
    // Do something on a specific pixel in the image in constant time
  }
}

O (n) O (n ^ 2), n , . , "n ^ 2" . , , "n" , .

+1

, , n-3 - j, 3. n , . , , , . , . !

0

Formally, you can use Sigma Notation to get the exact number of iterations and, therefore, determine the growth order.

enter image description here

0
source

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


All Articles