views:

101

answers:

3

I have question that comes from a algorithms book I'm reading and I am stumped on how to solve it (it's been a long time since I've done log or exponent math). The problem is as follows:

Suppose we are comparing implementations of insertion sort and merge sort on the same machine. For inputs of size n, insertion sort runs in 8n^2 steps, while merge sort runs in 64n log n steps. For which values of n does insertion sort beat merge sort?

Log is base 2. I've started out trying to solve for equality, but get stuck around n = 8 log n.

I would like the answer to discuss how to solve this mathematically (brute force with excel not admissible sorry ;) ). Any links to the description of log math would be very helpful in my understanding your answer as well.

Thank you in advance!

+1  A: 

Your best bet is to use Newton;s method.

http://en.wikipedia.org/wiki/Newton%27s_method

Visage
This will converge very quickly on an answer, especially since only integer values of n make sense. Stop iterating when (int)a == (int) b
Stefan Kendall
+3  A: 

http://www.wolframalpha.com/input/?i=solve(+8+log2+n+%3D+n,+n+)

Tom Sirgedas
+1 Beat me to it!
Vitor Py
Um, can anyone explain what the heck is happening there? The graph is great but we already know the answer was around 44. I wanted to try and understand the math of how to get there. (without wolfram ;)
Dr.HappyPants
You can't solve it exactly. You need to use numerical methods to get the decimal answer.
Tom Sirgedas
Can anyone at least explain the equation wolfram alpha uses?
Dr.HappyPants
The W function is defined as the inverse of the function f(W) = W*e<sup>W</sup>. So really, wolfram's "answer" is really just another equation (but a standard one, I guess). I suppose the square root function is kind of the same, in that it's the inverse function of f(x) = x*x. http://mathworld.wolfram.com/LambertW-Function.html
Tom Sirgedas
+1  A: 

One technique to solving this would be to simply grab a graphing calculator and graph both functions (see the Wolfram link in another answer). Find the intersection that interests you (in case there are multiple intersections, as there are in your example).

In any case, there isn't a simple expression to solve n = 8 log₂ n (as far as I know). It may be simpler to rephrase the question as: "Find a zero of f(n) = n - 8 log₂ n". First, find a region containing the intersection you're interested in, and keep shrinking that region. For instance, suppose you know your target n is greater than 42, but less than 44. f(42) is less than 0, and f(44) is greater than 0. Try f(43). It's less than 0, so try 43.5. It's still less than 0, so try 43.75. It's greater than 0, so try 43.625. It's greater than 0, so keep going down, and so on. This technique is called binary search.

Sorry, that's just a variation of "brute force with excel" :-)

Edit:

For the fun of it, I made a spreadsheet that solves this problem with binary search: binary‑search.xls . The binary search logic is in the second data column, and I just auto-extended that.

Joey Adams