• 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

    • Playground Games confirms Forza Horizon 6 save wipe bug by Taras Buria Forza Horizon 6 was launched last month to critical acclaim (check out our review here), and it became a smash hit in an instant. Now, weeks into the launch, with die-hard fans clocking hundreds of hours, Forza Horizon 6 is facing a serious issue: save wipes. After multiple complaints on Reddit and social media, the studio issued a statement. The problem with missing saves came shortly after Playground Games promised the initial batch of gameplay tweaks and improvements. Unfortunately, there seems to be no temporary fixes for those affected by unexpected save wipes. However, the studio published a new support document with a few important steps users should try. First, affected gamers should open a support ticket immediately (go here to file one) so that the support team can try recovering the lost progress by reverting to an earlier save. Playground Games says this should be done the same day the issue occurs. Meanwhile, gamers are urged not to start new play sessions or create new saves. The studio also published a few things gamers should try to avoid to prevent potential progress loss: Ensure your Gaming Services app on PC or XBOX Series X|S console is fully up to date. On XBOX Series X|S consoles, disable Quick Resume for Forza Horizon 6: To disable Forza Horizon 6 from using Quick Resume, highlight the game box art anywhere in the console experience (Home, My Games & Apps, Pins, etc) and then press the Menu button, then go to Manage game and add-ons > Quick Resume settings > Disable Quick Resume. Ensure you are online when ‘quitting’ the game. Give your saved time to sync to the cloud before powering off or switching devices. Do not force quit the game during save screens. Do not power off the device during gameplay. Always "Quit" (console) or "Exit to desktop" (PC) once you've finished your play session, ensuring the save icon is not visible when you’re closing the game. Before turning off your console, shutting down your PC, or force-closing the Steam app, give your devices or clients at least a few minutes to ensure your latest progress has been synchronized with the cloud. This will reduce the risk of progress reversions as you switch between different platforms. XBOX Series X|S consoles, Steam, and the XBOX app on PC all include game save indicators that confirm your progress has been synced. You can read more about the bug in the official support document here. Forza Horizon 6 is currently available on PC (Steam and the Microsoft Store), Xbox Series X|S, and Game Pass. The game is also coming to PlayStation 5 later this year.
    • If only Windows would have a toggle switch labeled "Get the latest updates as soon as possible" inside Windows Update settings... But nah, let's hide the new stuff inside a controlled feature rollout, even if the user is explicitly asking for the new stuff as soon as possible. Awesome idea!
    • After watching the Apple event earlier this week it is quite the contrast. Apple is going back and tweaking the code to make things more efficient in many areas of MacOS. Windows is boosting your electric build to hide their issues.
  • Recent Achievements

    • One Year In
      slackerzz earned a badge
      One Year In
    • One Year In
      highriskpaym earned a badge
      One Year In
    • One Month Later
      highriskpaym earned a badge
      One Month Later
    • Week One Done
      highriskpaym earned a badge
      Week One Done
    • Week One Done
      FBSPL earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      502
    2. 2
      PsYcHoKiLLa
      199
    3. 3
      +Edouard
      157
    4. 4
      Steven P.
      84
    5. 5
      ATLien_0
      74
  • Tell a friend

    Love Neowin? Tell a friend!