《算法导论》第二章-思考题(参考答案)

本文详细分析了《算法导论》第二章中的思考题,包括比较插入排序与归并排序在不同问题规模下的运行时间,讨论了在何时使用插入排序能提高效率,以及证明了冒泡排序的正确性和运行时间。此外,还探讨了霍纳规则在多项式评估中的应用及其与朴素算法的效率对比,并介绍了如何计算数组中的逆序对数量。
摘要由CSDN通过智能技术生成

算法导论(第三版)参考答案:思考题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 of n for which the modified algorithm has the same running time as standard merge sort, in terms of Θ -notation?
4. How should we choose k in practice?

第1问:

因为插入排序的最坏渐进时间为 Θ(n2) , 即 ak2+bk+c . 所以对 n/k 个长度为 k 的数组,总的时间为:

nk(ak2+bk+c)=ank+bn+cnk=Θ(nk)

第2问:

想象一下归并排序分析用到的递归树。此时叶子节点代价不再是 c ,而是 ck ,所以递归树高 lg(n/k) 。合并总代价为 cnlg(n/k) ,即最坏渐进时间为 Θ(nlg(n/k))

第3问:

Θ(nk+nlg(n/k))=Θ(nlgn)Θ(k+lg(n/k))=Θ(lgn)

k<lgn ,两者渐进时间相同为 Θ(nlgn) k 最大值为 lgn

第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
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值