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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...