tags:

views:

255

answers:

7
void compute(int n) {
        int h = n;
        while (h > 1) {
            for (int i = 0; i < n; i++) {
                // do some operation
            }
            h = h / 2;
        }
 }

Can anybody please tell me what is the complexity( Big O ) of this function of n ??

This is actually an argument between me and a friend of mine. my stand: complexity is O(n*log(n)) friend's stand: log(n)

Thanks for your responses.

+6  A: 

If this is homework (and it sounds a bit like it), then you should first try yourself.

Basically to get the complecity you look at the structure of the function, that is loops, nesting loops, etc. and determine how long they run, which inputs they depend on, etc.

In this case you have only one input, n. The local variable h starts with the same value as n, so it's essentially the same, complexity-wise, however, you need to keep track of how it's used.

You have essentially two nested loops here, one that runs to n, another one around it, that causes h to halve each time it's run. So this function is in O(n · log2n).

Joey
The second loop doesn't run to n/2. Try n = a million. The second loop is not going to run half a million times. In fact I think this is O < O(n.sqrt(n))
Kirk Broadhurst
Agreed, it's not n/2 which is why I deleted (and then edited) it, but no, it's not the root, see Botz3000's answer. Try n = 1024, you'll see that you get 10 iterations for the outer loop which is log_2 1024, not √1024 (which would be 32).
Joey
It helps to notice that h decreases at the same rate as the remaining nodes in a binary tree search, which is commonly known to run at O(log n)
Botz3000
Yes, when putting a little thought into it, it certainly becomes clear. I wonder, though, whether I should integrate the starting blurp into your answer, as you were the first with the correct one.
Joey
Hmm, nevermind.
Botz3000
I should have changed my answer sooner, I got voted down whilst you got voted up!
Kirk Broadhurst
I'm wondering, too, since my answer isn't as elaborate as yours. But it got me a badge :)
Botz3000
A: 

It appears to be O(n.sqrt(n))

The outer loop is obviously n, and the inner loop is sqrt(n).

EDIT: The inner loop is correctly log(n), because the number of iterations is x where 2^x = n.

Kirk Broadhurst
I should clarify: because the inner loop is 'halving' each time, it is order n^(1/2), which is the same as sqrt(n).
Kirk Broadhurst
Ah, I see. My mistake, I was wrong on the n/2 part. But you're unfortunately equally wrong on the root. See Botz3000's answer.
Joey
+15  A: 

I'd say since in every run, h is halved and n operations are done, it's O(n * log n).

Botz3000
+4  A: 

Some operation:

O(x)

The for loop: because n >= h and supposing h will not be modified during "some operation":

O(n*x)

The outer while loop:

O(log(n)*n*x)
lbp
In Big-O-Notation, you can discard the constant x.
Botz3000
That is, if x is a constant.
lbp
true, but i think he wouldn't have left the "x" operation commented out if it wasn't constant.
Botz3000
A: 

Execution time graphs:

complexity3

complexity4

A: 

Anyone who says this function is log(n) is not your friend.

Dan
A: 

It is clearly n*log(h).

fastcodejava