Jump to content

Big O revision


Recommended Posts

I hate this stuff but have to revise it for an exam.

 

Unfortunately I have examples and no answers!

 

 

This is an example from a previous paper. Worth a staggering 1 mark.

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

 

O(2N) Any chance this is right?

Link to comment
Share on other sites

<!--QuoteEBegin--><!--QuoteEBegin-->for(int i=0; i<n; i++)<!--QuoteEBegin-->   sum++;<!--QuoteEBegin-->for(int j=0; j<n; j++)<!--QuoteEBegin-->   sum++;<!--QuoteEBegin--><!--QuoteEBegin-->

 

O(2N)  Any chance this is right?

 

O(2N) ???

 

why did u use "for" loop???

Edited by dino_bosnia
Link to comment
Share on other sites

The general rule when dealing with Big O notation is to drop the constant before the variable as the difference it makes is negligible. Therefore, it is just

 

O(n)

 

Not too bad for an algorithm.

 

As for whether that is correct or not, from the looks of it yes. The two loops are independent of one another, therefore, they do not really have any affect on one another. So yeah, you have O(2n) which is then simplified down to just O(n).

 

If the two loops where nested then you would have O(n^2) - O n-squared.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...