第二章:算法基础
2-1 Insertion sort on small arrays in merge sort
Although merge sort runs in θ(nlgn) worst-case time and insertion sort runs in θ(n^2) worst-case time, the constant factors in insertion sort can make it faster in practice for small problem sizes on many machines. Thus, it makes sense to coarsen the leaves of the recursion by using insertion sort within merge sort when subproblems become sufficiently small. Consider a modification to merge sort in which n=k sublists of length k are sorted using insertion sort and then merged using the standard merging mechanism, where k is a value to be determined.
a. Show that insertion sort can sort the n=k sublists, each of length k, in θ(nk) worst-case time.
b. Show how to merge the sublists in θ(n lg(n/k)) worst-case time.
c. Given that the modified algorithm runs in θ(nk + n lgn/k)) worst-case time, what is the largest value of k as a function of n for which the modified algorithm has the same running time as standard merge sort, in terms of ‚θ notation?
d. How should we choose k in practice?
答:
a.
插入排序过程如下:
在串行的情况下,排序一个长度为k的数组需要θ(k^2),n/k个的话执行执行θ(k^2 * n/k) = θ(nk)。
本书在此处还没有讲多线程算法,所以暂时没有用多线程的思维来分析这道题。如果这道题用多线程的角度来分析的话,对n/k个长度为k的数组排序,那么总共的时间复杂度只有θ(k^2)而已,这里因为n/k个数组可以并发执行。
b.
这个问题需要参考下面这幅图:
这是标准的归并算法的merge的过程。最后求出标准归并算法的时间复杂度时,是用层高 乘以 每层的代价。
其实本题所求代价也用层高 乘以 每层的代价,只是层高没有那么高,因为当到子数组的规模到了n/k的时候,此时的层高应为log(n/k + 1)。故总代价为:lg(n/k+1) * n ,即为:θ(nlg(n/k))
c.
我解答的过程是首先感受代了几个值感受了一下,
- 当 k = 1 时,θ(nk + n lg(n/k)) = θ(n+ n lgn) = θ(n lgn),此时和归并算法的时间复杂度一样,但是k取得是最小值。
- 当 k = n/2 时,θ(nk + n lg(n/k)) = θ((n^2)/2+ n lg2)= θ(n^2),此时为θ(n^2),大于了归并算法。
很容易发现关键就是k和lgn之间的大小关系,则让 k = θ(lgn) (题中要求用θ符号),则可得时间复杂度:θ(nk + n lg(n/k)) = θ(n lgn + n lg (n/lgn)) =θ(n lgn)(其中 lg(n/lgn) < lgn)。
故最终答案:k = θ(lgn)
其实这道题我是解出来了的。但是我不知道这个答案要什么形式。现在回想起来就比较傻了,因为书说了要用θ记号来表示。我不应该强迫自己的
d.
解答可以参考: