• 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

    • Any so called performance increase will be in milliseconds, which nobody will actually notice in real world usage.
    • All it does is use the CPU more efficiently during boot to speed up boot times. That's it. Yawn....
    • It's not a one or the other kind of thing. Software should run efficiently, and the operating system should appropriately manage the CPU clocks. You could have the best most optimized software on earth, and it will still run faster if the CPU does a better job of boosting as needed. All this is doing is pre-boosting the CPU based on user actions, instead of waiting for the normal detection mechanism to kick in. If the OS knows it is about to need more CPU, why shouldn't it use that knowledge? It's the same idea of downshifting before passing someone, instead of just burying your foot into the peddle and waiting for the transmission to figure out what you want to do.
    • Audacity 3.7.8 by Razvan Serea Audacity is a free, open source digital audio editor and recording application. Edit your sounds using cut, copy, and paste features (with unlimited undo functionality), mix tracks, or apply effects to your recordings. The program also has a built-in amplitude-envelope editor, a customizable spectrogram mode, and a frequency-analysis window for audio-analysis applications. Built-in effects include bass boost, wah wah, and noise removal, and the program also supports VST plug-in effects. You can use Audacity to: Record live audio. Record computer playback on any Windows Vista or later machine. Convert tapes and records into digital recordings or CDs. Edit WAV, AIFF, FLAC, MP2, MP3 or Ogg Vorbis sound files. AC3, M4A/M4R (AAC), WMA and other formats supported using optional libraries. Cut, copy, splice or mix sounds together. Numerous effects including change the speed or pitch of a recording. Write your own plug-in effects with Nyquist. And more! See the complete list of features. Audacity 3.7.8 changelog: #10688 Fixed an exception thrown when pasting into a newly-created track (Thanks, David Bailes (@DavidBailes)!) #10870, #10884, #10775, #10629 Fixed tone generation, waveform-scale setting, SetClip Name parameter, and clip-boundary command names for scripting and macros (Thank you, David Bailes (@DavidBailes)!) #11106 Fixed the loading of presets for the Distortion effect (A million thanks, David Bailes (@DavidBailes)!) #10947 Fixed paste into an empty audio track not preserving the source sample rate (Thanks, Juan Gabriel Colonna (@juancolonna)!) #10776 Allowed AltGr modifier in label and clip name editing (Thanks, Davide Peressoni (@DPDmancul)!) #9938 Added options to choose where silence is truncated (start/middle/end) (Thanks, Noah Rosenfield (@nosenfield)!) #9935 Added Podcast 2.0 chapters JSON export for label tracks (Thanks, Noah Rosenfield (@nosenfield)!) #10103 Improve UI on HiDPI displays on Linux/wxGTK (Thanks, Ivan A. Melnikov (@iv-m)!) #10099 Fixed MixerBoard Mute and Solo button display (Thanks, Ivan A. Melnikov (@iv-m)!) #10681 Fixed multichannel FLAC import #10999 Fixed envelope being broken after joining clips Download: Audacity 64-bit | Standalone ~20.0 MB (Open Source) Download: Audacity 32-bit | Standalone Download: Audacity ARM64 | Standalone View: Audacity Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • There really isn't anything magical about the low latency profile, other OS's do this as well. All they're doing is using your CPUs boost clock options in a more smarter way.
  • Recent Achievements

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

    1. 1
      +primortal
      497
    2. 2
      PsYcHoKiLLa
      198
    3. 3
      +Edouard
      155
    4. 4
      Steven P.
      84
    5. 5
      ATLien_0
      71
  • Tell a friend

    Love Neowin? Tell a friend!