Choosing minimum length k of array for merge sort where use of insertion sort to sort the subarrays is more optimal than standard merge sort

我怕爱的太早我们不能终老 提交于 2019-12-06 15:17:44

Well, this is a mathematical minimization problem, and to solve it, we need some basic calculus.

We need to find the value of k for which d[n*k + n*lg(n/k)] / dk == 0.

We should also check for the edge cases, which are k == n, and k == 1.

The candidate for the value of k that will give the minimal result for n*k + n*lg(n/k) is the minimum in the required range, and is thus the optimal value of k.


Attachment, solving the derivitives equation:

d[n*k + n*lg(n/k)] / dk = d[n*k + nlg(n) - nlg(k)] / dk
= n + 0 - n*1/k = n - n/k 
=>
n - n/k = 0 => n = n/k => 1/k = 1 => k = 1

Now, we have the candidates: k=n, k=1. For k=n we get O(n^2), thus we conclude optimal k is k == 1.


Note that we found the derivitives on the function from the big Theta, and not on the exact complexity function that uses the needed constants.
Doing this on the exact complexity function, with all the constants might yield a bit different end result - but the way to solve it is pretty much the same, only take derivitives from a different function.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!