• 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

    • I agree when are you going to read this (really poor BTW) article? Here is a better article so you actually know what is going on and answers questions you had in other comments --> https://arstechnica.com/gadgets/2026/05/speed-boosting-low-latency-profile-is-one-of-the-improvements-coming-to-windows-11/ It is unclear if one will be able to disable the new profile at this point but I am not seeing any reason why one would.
    • I disagree; they come off very "bitchy" and "whiny". Make a great product and combine that with a great price (free) and people will come over to your side. Or build it and they will come as they say. Constantly trying to get attention by complaining all the time, will turn people off to your product.
    • It use to be a nightmare, with LibreOffice supporting a newer draft ODF standard by default, and Microsoft Office supporting the older non-draft standard. Now that they both support the same version of ODF, they should be interoperable.
    • Brave Browser 1.91.171 by Razvan Serea Brave Browser is a lightning-fast, secure web browser that stands out from the competition with its focus on privacy, security, and speed. With features like HTTPS Everywhere and built-in tracker blocking, Brave keeps your online activities safe from prying eyes. Brave is one of the safest browsers on the market today. It blocks third-party data storage. It protects from browser fingerprinting. And it does all this by default. Speed - Brave is built on Chromium, the same technology that powers Google Chrome, and is optimized for speed, providing a fast and responsive browsing experience. Brave Browser also features Brave Rewards, a system that rewards users with Basic Attention Tokens (BAT) for viewing opt-in ads. This innovative system provides an alternative revenue model for content creators and a way to support the Brave community. SlimBrave Neo takes all the good things about Brave and makes them even better by keeping everything clean, light, and privacy-focused. It removes the extra clutter, turns off features you might not need, and cuts down on anything that could slow you down or collect unnecessary data. Because it relies on simple settings and policies instead of modifying the browser itself, you still get full Brave compatibility—just in a smoother, lighter, and more privacy-friendly package. Brave Browser 1.91.171 changelog: General Fixed Cardano not being disabled on upgrade to Brave Origin. Upgraded Chromium to 149.0.7827.103. Origin Removed “Survey Panelist” setting from brave://settings/privacy. Fixed P3A and usage ping under brave://settings/privacy being displayed on first launch on Linux. Upgraded Chromium to 149.0.7827.103. Download: Brave Browser 64-bit | 1.2 MB (Freeware) Download: Brave Browser 32-bit View: Brave Homepage | Offline Installers | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Hi. As the title suggests, I can't access the forum on my phone. I'm using Edge on Android and when I try to navigate to the forum I get a "we value your privacy" popup and none of the buttons are clickable. It effectively stonewalls me from reading any forum content.
  • Recent Achievements

    • One Month Later
      Jamswaz earned a badge
      One Month Later
    • Week One Done
      Jamswaz earned a badge
      Week One Done
    • Rookie
      Marzoid went up a rank
      Rookie
    • Community Regular
      coch went up a rank
      Community Regular
    • One Year In
      slackerzz earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      519
    2. 2
      PsYcHoKiLLa
      190
    3. 3
      +Edouard
      156
    4. 4
      Steven P.
      84
    5. 5
      ATLien_0
      75
  • Tell a friend

    Love Neowin? Tell a friend!