Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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|improve this question
    
Note: n belongs to the natural numbers –  Tom Mar 1 '12 at 17:42
1  
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

1 Answer 1

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|improve this answer

Your Answer

 
discard

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.