views:

61

answers:

2

Snape’s “Unfriendly Algorithms for Wizards” textbook claims the running time of merge sort is O(n^4). Is this claim correct?

Solution: Yes. This claim is technically correct, because O(n^4) only gives an upper bound for how long the algorithm takes. However, it’s an obnoxiously unhelpful answer, since the tight bound is Θ(n log n).

I'm not quite understanding what the solution is stating. How can O(n^4) be correct?

+5  A: 

Big O notation is an upperbound on the worst case for an algorithm runtime.

Since O(n^4) is above the worst case time of mergesort it is technically correct because it does provide a bound - ie. Mergesort will never have a performance worse than O(n^4).

However, it's unhelpful because a better expression of the running time is O(n log n), which is the "tightest" bound for merge sort

Martin
Ahh ok I see..so O(n^1000) would also be 'technically' correct?
fprime
Yes, because it's true that you won't ever get performance worse than O(n^1000) from mergesort
Martin
Yes, O(n^1000) is another, even less helpful upper bound.
Thilo
Ok while we're on this topic, if we say that mergesort is BigOmega of (x), then whats the 'english' way to think about that? What about BigTheta? Does BigTheta mean that its the maximum running time of the function?
fprime
BigOmega - It will not run faster than this. BigTheta - This is a combination of Big O and Big Omega, there's no "special" input that will make it run faster or slower.
Zareth
Wait-BigOmega: It will not run faster than this..So thats kind of the maximum runtime?
fprime
No, it will not run faster than this, so it's a minimum bound rather than a maximum bound. a rather important distinction!
Martin
I would describe Big Theta as "is eventually". I.e. for big enough n, runtime will be indistinguishable from (some multiple of) the function given, regardless of the content of the input.
j_random_hacker
+1  A: 

Big-O is a set, which includes everything that runs as fast as (foo) or faster. Little-O is a set of things that run strictly faster than (foo). While it's correct to say mergesort is O(n^4), it's not very useful, because it's Theta(n log n). Saying that mergesort is o(n^4) is marginally more useful, because little-o notation is never used to imply big-theta runtime.

Further complicating matters, big-O is often used when big-theta would be more appropriate, just because most keyboards don't have a theta.

Chris
Yes, it's actually just an (ingrained, institutionalised) CS habit to write f(n) = O(n^2) or similar -- from the start it should really have been written using the set membership symbol instead of an equals sign, but it's too late for that now it seems.
j_random_hacker