• 0

big O notation


Question

Hi. I learned the big O notation in class, but I still don't understand it. I have a test coming up and I was wondering if anybody could tell me how it is done. I have examples which my teacher gave us to practice on. I will post a few here and please could you tell me what I have to do and any tips and tricks?

a) for(j =0; j<=n, j+=2)

printf("Hello\n");

for (k=n-1; k>1; k-=3)

printf("Good Bye\n");

c) for(i=1; i<=n; i+=3)

for(j=i; i<= i+5; ++i)

sum++;

for(k=n; k>=0; --k)

printf("Result = %d\n", sum-k);

Link to comment
https://www.neowin.net/forum/topic/178833-big-o-notation/
Share on other sites

20 answers to this question

Recommended Posts

  • 0

Big O notation represents the max number of iterations(not really the word I want to use) of an algorithm.

For example if you have a program that just printed everything in an array it would be:

"On" because you are going though everything once

If you had a selection sort algorithm it would be:

"On^2" because of the nested for loops.

This is a pretty bad explanation... use google.

I think the two alogorithms you have there are both "On" because they both are linear. However, I could be wrong.

Edited by b0nk
Link to comment
https://www.neowin.net/forum/topic/178833-big-o-notation/#findComment-2295530
Share on other sites

  • 0

thanks bonk. I understand the point of big o notation and what you have said. But I don't understand the more complex examples such as using the summation notations for quadratic loops and dependent quadratic loops. I googled but I could not find a detailed tutorial on it, just the basics which I already know.

Link to comment
https://www.neowin.net/forum/topic/178833-big-o-notation/#findComment-2295610
Share on other sites

  • 0

It helps I taught this in college =)

It depends on your indexing.

If you are ADDING or SUBTRACTING the same value (m) to your index (i) your notation will be: O(i / m)

If you are MULTIPLING or DIVIDING the same value (m) to your index (i) the notation will be: O(log(i, base m))

So:

b) i = n

while (i>0)

{

pirntf("Fun Times\n'");

i/=2;

}

will end up being:

log ( n ) - log is usually base 2 in computer science but you should specify it.

The other way I tell people is to write out iterations; For the while loop you will have:

n | i

0 |

1 | 1

2 | 2 1

3 | 3 1

4 | 4 2 1

5 | 5 2 1

6 | 6 3 1

7 | 7 3 1

8 | 8 4 2 1

Notice how you add a new digit every power of 2 - if you keep doing this you will get a logrithmetic graph.

Link to comment
https://www.neowin.net/forum/topic/178833-big-o-notation/#findComment-2295696
Share on other sites

  • 0

My guess would be:

b) O(log(n))

d) O(n!)

Its somehow hard to explain. But is has less to do which loop you deal with but what you do with the variables in the loop.

Edit: Ah. I see pballsim took his time to explain this. Good explanation! I suck at explaining this kinda stuff because i don't exactly know how i do it myself. I just see it somehow...

Link to comment
https://www.neowin.net/forum/topic/178833-big-o-notation/#findComment-2295741
Share on other sites

  • 0

thanks a lot! I have learned somewhere to use summation notations. Could you teach me how to use summation notations? I have an example in my lecture notes on dependent quadratic, but I dont really understand how to use it.

i = 1;

loop(i <=10)

j=1

loop(j<=i)

application code

j=j+1;

end loop

i=i+1;

endloop

the answer it says is n(n+1)/2 but i dont understand how they got this.

Link to comment
https://www.neowin.net/forum/topic/178833-big-o-notation/#findComment-2295806
Share on other sites

  • 0

Big O notation you ignore contants... so everybody will give you O(n^2)

But the reason they get that is the loop goes as follows how it's adding the numbers. It's essentially adding 1, then 1 2, then 1 2 3, and based off of Gauss's formula for adding:

1 2 3 4 ... n

efficiently is: n(n+1) / 2

Link to comment
https://www.neowin.net/forum/topic/178833-big-o-notation/#findComment-2295871
Share on other sites

  • 0

Hehe I was confused on Big O Big Omega Big yada yada when I was taking discrete math class. It was one of the worst class I ever taken. The concept is easy, and yet there is a "definite" answer for each type of problem.

I bailed out that class the first time also because my prof doesn't speak English well at all. I tried it the second time, and got a B- in that class (Odd enough, my prof encuraged me to quit.. wow the first time eh?). Still that Big O concept has always been rough to me. There is no good textbook out there that make your life easier either.

Pretty much after that class, I quit CS major. Now that i look back, I haven't regret yet.

So yeah, that's what happened to me at University of Michigan Ann Arbor (PS don't go there if you are out of state, you won't get much financial aids.)

Edited by ThunderRiver
Link to comment
https://www.neowin.net/forum/topic/178833-big-o-notation/#findComment-2302554
Share on other sites

  • 0

You have to look at how the variables are. See my previous post. If your indexing is increasing/decreasing by a constant in any kind of loop then it's O(n/constant).

A for loop is not always O(n). E.g.

for (int i = n; i > 0; i /= 2)

this is an O(log(n))

A for loop, while loop and do while loops are exactly identilcal and can be written using one another. A for loop is just syntax sugar for:

void foo()
{
 ? // ...
 ?{
 ? ? int i = n;
 ? ? while (i &gt; 0)
 ? ?{
 ? ? ? ?i /= 2;
 ? ?}
 ? ?// ...
}

Except if you don't use the { before the int i then you can use 'i' after the loop. But in a for loop you cannot use i after you declare it. You would have to declare it before you define your for loop.

Link to comment
https://www.neowin.net/forum/topic/178833-big-o-notation/#findComment-2303314
Share on other sites

  • 0

Hey, I took that exact class! (I go to berkeley) Such a waste of a semester. Let me find the notes for the CS class on the same topic.

http://www.cs.berkeley.edu/~jrs/61b/lec/20

edit: if you have a postscript reader, this will have better graphs: http://www.cs.berkeley.edu/~jrs/61b/lec/20.ps

(they are a lot more formal, but if you understand the formalities, it's a lot easier in practice.)

Link to comment
https://www.neowin.net/forum/topic/178833-big-o-notation/#findComment-2310083
Share on other sites

  • 0

So to understand this Big O thing, I only have to know this:

"It depends on your indexing.

If you are ADDING or SUBTRACTING the same value (m) to your index (i) your notation will be: O(i / m)

If you are MULTIPLING or DIVIDING the same value (m) to your index (i) the notation will be: O(log(i, base m))"?

I only have to look at indexing right?

Link to comment
https://www.neowin.net/forum/topic/178833-big-o-notation/#findComment-2329304
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • So what is it, "Some exciting upgrades happened under the hood, and one of those upgrades is now available to all Windows 11 users." or "Also, you may need to enable it manually, as new features are rolling out gradually. Here is how to do it:" We keep hearing these are rolling out but they never seem to show up, we have force them on with ViVetool. Getting a bit tired of it.
    • Seems very lazy, so basically drain more battery power on laptops/tablets if this is turned on.
    • Edifier's popular R1280Ts bookshelf speaker can be a nice upgrade for your PC desktop audio by Sayan Sen Yesterday we covered a very nice discount on Sony's high-resolution SS-CS5M2 speakers, which are currently on sale for just $178. It packs the rather rare super tweeter that offers an additional level of clarity and "airy"-ness which a tweeter cannot provide. It's a passive speaker though which means it will require external amplification, which will cost extra. Let's say though that you have a budget of under $150 but still want a decent-sounding speaker that's active. The Edifier 1280Ts can help in this regard, as the unit is currently at a decent price of $130 (purchase link under the specs table down below). While you will not get deep sub-bass from the 1280Ts, you should get clearer vocals and highs like cymbals than a cheaper satellite-based 2.1 system. Obviously the soundstage and imaging will also improve due to the better reproduction of highs. As mentioned above, the Edifier R1280Ts is active and so does not need a separate amplifier, as it's a powered system with its own amplification. A great thing about this model is that you can add a separate active subwoofer to it too using the "sub out" option, which essentially acts like an LFE. This way, you can add in the missing deep bass. The technical specifications of the Edifier R1280Ts are given in the table below: Specification Value Total Output Power 42W RMS (21W + 21W) Driver Units 4-inch mid-range/bass driver + 0.5-inch silk diaphragm treble driver Frequency Response 52Hz – 20kHz Signal-to-Noise Ratio (SNR) ≥85dBA Input Sensitivity Line In1: R/L: 500±50mV Line In2: R/L: 700±50mV Inputs Dual RCA inputs Outputs Sub Out port for external subwoofer Get it at the link below: Edifier R1280Ts Powered Bookshelf Speakers - 2.0 Stereo Active Near Field Monitors with Subwoofer Line Out: $129.99 (Sold by Edifier Online Store, Shipped by Amazon US) This Amazon deal is US-specific and not available in other regions unless specified. This is a first-party seller link (at the time of article publishing); ensure that you also purchase from a first-party seller link only. If you don't like it or want to look at more options, check out the previous deals that we have covered, OR you can also visit Amazon US deals page. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
    • Appreciate the focus on UI performance, but this is going the wrong way. Instead of optimizing performance, coding to lower latency, etc. this is just throwing horsepower. This is lazy.
  • Recent Achievements

    • Week One Done
      FBSPL earned a badge
      Week One Done
    • One Year In
      Jim Dugan earned a badge
      One Year In
    • One Month Later
      Tommi118 earned a badge
      One Month Later
    • One Month Later
      sjbousquet earned a badge
      One Month Later
    • Week One Done
      sjbousquet earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      489
    2. 2
      PsYcHoKiLLa
      197
    3. 3
      +Edouard
      155
    4. 4
      Steven P.
      84
    5. 5
      ATLien_0
      69
  • Tell a friend

    Love Neowin? Tell a friend!