算法导论(第三版)参考答案:思考题2.1,思考题2.2,思考题2.3,思考题2.4
Problem 2-1
(Insertion sort on small arrays in merge sort ) Although merge sort runs in Θ(nlgn) worst-case time and insertion sort runs in Θ(n2) 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.
1. Show that insertion sort can sort the n/k sublists, each of length k, in Θ(nk) worst-case time.
2. Show how to merge the sublists in Θ(nlg(n/k)) worst-case time.
3. Given that the modified algorithm runs in Θ(nk+nlg(n/k)) worst-case time, what is the largest value of k as a function ofn for which the modified algorithm has the same running time as standard merge sort, in terms of Θ -notation?
4. How should we choosek in practice?
第1问:
因为插入排序的最坏渐进时间为 Θ(n2) , 即 ak2+bk+c . 所以对 n/k 个长度为 k 的数组,总的时间为:
第2问:
想象一下归并排序分析用到的递归树。此时叶子节点代价不再是 c ,而是
第3问:
当 k<lgn ,两者渐进时间相同为 Θ(nlgn) 。 k 最大值为
第4问:
选择插入排序比归并排序快的最大数组长度作为 k 值。
Runtime comparison
I’m implemented this in C and in Python. I added selection for completeness sake in the C version. I ran two variants, depending on whether merge()
allocates its arrays on the stack or on the heap (stack won’t work for huge arrays). Here are the results:
STACK ALLOCATION
================
merge-sort = 0.173352
mixed-insertion = 0.150485
mixed-selection = 0.165806
HEAP ALLOCATION
===============
merge-sort = 1.731111
mixed-insertion = 0.903480
mixed-selection = 1.017437
Here’s the results I got from Python:
merge-sort = 2.6207s
mixed-sort = 1.4959s
I can safely conclude that this approach is faster.
C runner output
merge-sort = 0.153748
merge-insertion = 0.064804
merge-selection = 0.069240
Python runner output
merge-sort = 0.1067s
mixed-sort = 0.0561s
C code
#include <stdlib.h>
#include <string.h>
#define INSERTION_SORT_TRESHOLD 20
#define SELECTION_SORT_TRESHOLD 15
void merge(int A[], int p, int q, int r) {
int i, j, k;
int n1 = q - p + 1;
int n2 = r - q;
#ifdef MERGE_HEAP_ALLO