Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Suppose we are comparing implementations of insertion sort and merge sort on the same machine. For inputs of size $n\in\mathbb{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?

share|cite|improve this question
Note: n belongs to the natural numbers – Tom Mar 1 '12 at 17:42
It's equivalent to solving $n<8\log(n)$. Assuming the logarithm is 2-based, if $2\leq n\leq 32$, the inequality holds. The exact range can be approximated numerically. – Hawii Mar 1 '12 at 17:50
up vote 2 down vote accepted

Solving $8n^2 \lt 64n\log(n)$ (equivalently, $n\lt 8\log(n)$) exactly requires use of Lambert's W function. Solving it approximately is pretty easy.

At $n=1$, $8n^2\gt 64n\log(n)$; at $n=2$, $8n^2 \lt 64n\log(n)$; if $\log$ is base $2$, then they cross again at about $n=45$.

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.