18

I was reading about Big O notation. It stated,

The big O of a loop is the number of iterations of the loop into number of statements within the loop.

Here is a code snippet,

for (int i=0 ;i<n; i++)
{
    cout <<"Hello World"<<endl;
    cout <<"Hello SO";
}

Now according to the definition, the Big O should be O(n*2) but it is O(n). Can anyone help me out by explaining why is that? Thanks in adavance.

  • 2
    There is only n iteration inside the loop – TheOneTeam Aug 06 '11 at 15:17
  • 1
    But there are 2 statements in the loop –  Aug 06 '11 at 15:20
  • 1
    @JustAnotherProgrammer see my answer, the statements in the loop take constant time, two constants make another constant. What matters is the number of iterations in the loop. – Griffin Aug 06 '11 at 15:24
  • 1
    possible duplicate of [Plain English explanation of Big O](http://stackoverflow.com/questions/487258/plain-english-explanation-of-big-o) – H H Aug 06 '11 at 15:24
  • No No its Big O will be O(n) as we go with time equation the loop runs for n+1 time so big O will be n not n2.. – Ahsan Raza Oct 30 '16 at 12:17

7 Answers7

22

If you check the definition of the O() notation you will see that (multiplier) constants doesn't matter.

The work to be done within the loop is not 2. There are two statements, for each of them you have to do a couple of machine instructions, maybe it's 50, or 78, or whatever, but this is completely irrelevant for the asymptotic complexity calculations because they are all constants. It doesn't depend on n. It's just O(1).

O(1) = O(2) = O(c) where c is a constant.
O(n) = O(3n) = O(cn)
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • The complexity is measured towards an instruction(maybe not the right phrase but I don't know a better). If he wants to measure the complexity of std::cout's he is right. If he wants to consider all lowlevel functions, then you are right. Despite that the constant does not matter as you pointed out. – Nobody moving away from SE Aug 06 '11 at 15:23
10

O(n) is used to messure the loop agains a mathematical funciton (like n^2, n^m,..).

So if you have a loop like this

  for(int i = 0; i < n; i++) {
     // sumfin
  }

The best describing math function the loops takes is calculated with O(n) (where n is a number between 0..infinity)

If you have a loop like this

     for(int i =0 ; i< n*2; i++) {

     }

Means it will took O(n*2); math function = n*2

    for(int i = 0; i < n; i++) {
         for(int j = 0; j < n; j++) {

         }
    }

This loops takes O(n^2) time; math funciton = n^n This way you can calculate how long your loop need for n 10 or 100 or 1000

This way you can build graphs for loops and such.

blejzz
  • 3,349
  • 4
  • 39
  • 60
4

Big-O notation ignores constant multipliers by design (and by definition), so being O(n) and being O(2n) is exactly the same thing. We usually write O(n) because that is shorter and more familiar, but O(2n) means the same.

hmakholm left over Monica
  • 23,074
  • 3
  • 51
  • 73
3

First, don't call it "the Big O". That is wrong and misleading. What you are really trying to find is asymptotically how many instructions will be executed as a function of n. The right way to think about O(n) is not as a function, but rather as a set of functions. More specifically:

O(n) is the set of all functions f(x) such that there exists some constant M and some number x_0 where for all x > x_0, f(x) < M x.

In other words, as n gets very large, at some point the growth of the function (for example, number of instructions) will be bounded above by a linear function with some constant coefficient.

Depending on how you count instructions that loop can execute a different number of instructions, but no matter what it will only iterate at most n times. Therefore the number of instructions is in O(n). It doesn't matter if it repeats 6n or .5n or 100000000n times, or even if it only executes a constant number of instructions! It is still in the class of functions in O(n).

To expand a bit more, the class O(n*2) = O(0.1*n) = O(n), and the class O(n) is strictly contained in the class O(n^2). As a result, that loop is also in O(2*n) (because O(2*n) = O(n)), and contained in O(n^2) (but that upper bound is not tight).

Mikola
  • 9,176
  • 2
  • 34
  • 41
2

O(n) means the loops time complexity increases linearly with the number of elements.

2*n is still linear, so you say the loop is of order O(n).

However, the loop you posted is O(n) since the instructions in the loop take constant time. Two times a constant is still a constant.

Griffin
  • 13,184
  • 4
  • 29
  • 43
0

The fastest growing term in your program is the loop and the rest is just the constant so we choose the fastest growing term which is the loop O(n)

In case if your program has a nested loop in it this O(n) will be ignored and your algorithm will be given O(n^2) because your nested loop has the fastest growing term.

-1

Usually big O notation expresses the number of principal operations in a function.

In this tou're overating over n elements. So complexity is O(n).

Surely is not O(n^2), since quadratic is the complexity of those algorithms, like bubble sort which compare every element in the input with all other elements.

As you remember, bubble sort, in order to determine the right position in which to insert an element, compare every element with the others n in a list (bubbling behaviour).

At most, you can claim that you're algorithm has complexity O(2n),since it prints 2 phrases for every element in the input, but in big O notation O(n) is quiv to O(2n).

user278064
  • 9,982
  • 1
  • 33
  • 46