when is insertion sort faster than merge sort?

后端 未结 2 744
猫巷女王i
猫巷女王i 2021-02-04 22:41

For a homework problem, I was told that insertion sort runs at 8n^2 and that merge sort runs at 64(n(lg n)). As part of the solution I was given, it said that insertion sort was

2条回答
  •  一生所求
    2021-02-04 23:14

    This is the second exercise question in Introduction to Algorithms, 3rd edition by Cormen. The solution to this equation is not so straight forward for a newcomer to algorithms:

    Insertion sort beats merge sort when 8n^2 < 64n lg n , n < 8 lg n , 2^ n/8 < n . This is true for 2 <= n <= 43 (found by using a calculator). Therefore, we can rewrite merge sort to use insertion sort for input of size 43 or less in order to improve the running time.

提交回复
热议问题