views:

242

answers:

5
+4  Q: 

Big O question

If I have the following code:

IterateArray(object[] array)
{
    for(int i=0; i<array.length; i++)
    {
        Dosomething(array[i]);
    }
}

and the Dosomething(object) method's time performance is O(log n), is the overall performance of IterateArray O(n) or O(n log n)?

Thanks.

+12  A: 

It would be O(n log n)

You're doing an O(log n) performance operation n times, and multiplication holds with Big O, so O(n) * O(log n) = O(n log n)

It's important to note that there really need not be any distinction between m and n if you're looking at two different sized arrays. The reason being is that m and n are both constants, and they are asymptotically equivalent if you were to graph their growth rates.

AlbertoPL
Not so fast, it would be O(n log n) if it was DoSomething(array.length) instead of DoSomething(array[i])
Juozas Kontvainis
The question states that Dosomething performs in log n time. Besides, I don't think passing in an integer into method call would make the difference.
AlbertoPL
To further my point, you cannot assume anything about the Dosomething method at all, even if you were passing in an entire array versus just a single element. Of course, your example demonstrates passing in an integer.
AlbertoPL
The n in O(n) is the size of the input. The input to DoSomething is array[i], not array. Since that could be anything (it's an object), it's wrong to assume that each invocation of DoSomething will take O(array.length).
ojrac
Imagine that array always contains zeros, except one element is equal to array.length. Do you still claim that performance is O(n log n) in this case?
Juozas Kontvainis
Yes, because Dosomething could be an iteration over a completely different array. The point is, we have no idea except to take the assumption that Dosomething is indeed O(log n), because the that's what the question states.
AlbertoPL
It's a homework question, `DoSomething`'s complexity is probably given. Way to do the kid's homework guys.
geowa4
@Juozas - Big-O is an estimation for the worst case, not a special case.
Not Sure
+10  A: 

O( n log n )

Think about it - you're performing a log n operation n times.

Not Sure
+1  A: 

Hi

Since the 'for' loop iterates n (say the array length is n) times and in each iteration 'Dosomething' is executed, the overall performance would be O(n logn).

cheers

Andriyev
+4  A: 

For each of your m objects, if the performance of DoSomething() is O(log n), then the total performance across all of your m objects would be O(m log n).

bcwood
+14  A: 

The short & somewhat wrong answer is O(n log n).

The long answer: It'd be more accurate to write it as O(n log m).

Unless DoSomething really DOES depend on the entire array, it looks like it's operating on a single element. So we distinguish this separately, using "m".

hythlodayr
+1, though I wouldn't even acknowledge O(n lg n) as an answer. There are two variables, and to express the running time in terms of just one can only be right by coincidence.
ojrac
I'm voting this up because it is more thorough than the other answers. However, given what the OP put in the question, I'm inclined to believe the correct answer is O(n log n). That may not be what the OP _meant_, but it is what they said.
rmeador
There's no real point in distinguishing constants. When it comes down to asymptotic analysis, you're not establishing any new threshold.
AlbertoPL
@AlbertoPL: Except you don't know the scales of M and N so you can't say n log n is the asymptote. If M>>>N, then even a log. growth is something to consider.
hythlodayr
This is a homework question, so I'm fairly confident that the complexity of `DoSomething` is given in the problem. Therefore, the answer is O(nlog(n)). I say shame for giving away a homework question so easily. The community should help the person think about it rather than do their homework for them.
geowa4
It may be something to consider practically, but from a theoretical standpoint a constant is always O(1), and two constants that are O(1) are asymptotically equivalent.
AlbertoPL
@AlbertoPL: Refer to radix sort. There is nothing constant about m and n, as they're not related to each other.
hythlodayr
Didn't quite come out right: I meant to sa, they're NOT the same constant or scales of a single constant.
hythlodayr
It does not matter that they are not the same constant at all, or even if they are the same scale. Even if m were 2 and n were 10000, they are still equivalents in Big O world, mathematically. It's the same reasoning that O(n^2) when n is 3 is of the same Big O family of O(n^2) when n is 100, or 1000.
AlbertoPL
I'm sorry, I have to downvote. After rereading your answer, you are making an assumption about the other function. Without seeing it, you cannot arbitrarily insert new variables.
geowa4