views:

3079

answers:

5

For homework, I was given the following 8 code fragments to analyze and give a Big-Oh notation for the running time. Can anybody please tell me if I'm on the right track?

//Fragment 1
for(int i = 0; i < n; i++)
    sum++;

I'm thinking O(N) for fragment 1

//Fragment 2
for(int i = 0; i < n; i+=2)
    sum++;

O(N) for fragment 2 as well

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

O(N^2) for fragment 3

//Fragment 4
for(int i = 0; i < n; i+=2)
    sum++;
for(int j = 0; j < n; j++)
    sum++;

O(N) for fragment 4

//Fragment 5
for(int i = 0; i < n; i++)
    for( int j = 0; j < n * n; j++)
        sum++;

O(N^2) for fragment 5 but the n * n is throwing me off a bit so I'm not quite sure

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

O(N^2) for fragment 6 as well

//Fragment 7
for(int i = 0; i < n; i++)
    for( int j = 0; j < n * n; j++)
        for(int k = 0; k < j; k++)
            sum++;

O(N^3) for fragment 7 but once again the n * n is throwing me off

//Fragment 8
for(int i = 1; i < n; i = i * 2)
    sum++;

O(N) for fragment 8

+12  A: 

I think fragment 5 is O(n^3), and similarly fragment 7 is O(n^5)*. It also looks like O(log(n)) for fragment 8.

For the n * n problems, you have to execute the body of the loop n * n times, so it would be O(n^2), then you compound that with the order of the other code. Fragment 8 actually doubles the counter instead of incrementing it, so the larger the problem, the less additional work you have to do, so it's O(log(n))

*edit: Fragment 7 is O(n^5), not O(n^4) as I previously thought. This is because both j and k go from 1 to n * n. Sorry I didn't catch this earlier.

Kyle Cronin
oh okay, I see what you're saying! So for 5, the n * n means that the body of the inner loop will be executed order of N^2 times and compounding that with the order of N for the outer loop, overall it will be O(N^3).
outsyncof
same idea for fragment 7. Man the fragment 8 went completely over my head. Sorry if this is asking too much, but I'm just curious what would the code for O(Nlog(N)) look like in case I ever run into it?
outsyncof
For an O(n*log(n)) algorithm, imagine an outer loop that counts from 1 to n and an inner loop that goes from 1 to n by multiplying the counter by 2 each time. However, it's extremely unusual to encounter such algorithms. The most common O(n*log(n)) algorithms are the QuickSort and Merge Sort.
Kyle Cronin
okay, so basically if I were to put an outer (1 to n) loop around fragment 8, I would end up with an O(n*log(n)) algorithm because the outer O(n) loop and inner O(log(n)) would compound to form it. Thank you for all of your help!
outsyncof
Yes. I'd code it up but comments don't display code well.
Kyle Cronin
Careful! Fragment 7 is O(n^5) not O(n^4) because the inner loop is also bounded by n^2.
Dave L.
Yikes, you're right Dave. I'll change my answer.
Kyle Cronin
As a little additional aside that you might already know, vpotok, fragment 8 runs about log base 2 n times for n items. But then again, no one really cares what base it's in, so we just say O(log n).
Andrew Szeto
@Andrew:What we does not care what base it's in?
Jichao
A: 

You appear to be on the right track. With regards to the N*N what effect do you think it would have? It is another factor of N so it would likely be a higher order.

Just a warning, I saw another post like this and it was extremely down voted. Be careful. Here is the post.

smaclell
I actually upvoted the question, since he (1) obviously has made a serious attempt at solving the problems and (2) was honest about it being a homework.
JesperE
Oh I definitely agree, this is a solid attempt at the problem and a valid question. I just want to warn him in case others react poorly. Although in this case he should be fine.
smaclell
+2  A: 

For case 8 try writing out the number of iterations for some values of N and see what the pattern looks like ... it isn't O(N)

Rob Walker
A: 

As things go thats a good homework problem. It gets your mind wrapped around the factors involved. I missed #8.

EvilTeach
+5  A: 

Fragment 7 is O(n^5), not O(n^4) as the currently accepted comment claims. Otherwise, it's correct.

Dave L.
Thanks for catching that
Kyle Cronin
Sure thing. That's the benefit of a collaborative site.
Dave L.