算法导论(三版):第二章思考题

本文详细解答了《算法导论》第三版第二章的四个问题,包括插入排序在归并排序中的应用、冒泡排序的正确性、霍纳规则的正确性和倒序对的计算。讨论了不同排序算法的最坏情况运行时间和如何选择插入排序的小数组阈值。
摘要由CSDN通过智能技术生成

第二章:算法基础

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)

参考:stack overflow

其实这道题我是解出来了的。但是我不知道这个答案要什么形式。现在回想起来就比较傻了,因为书说了要用θ记号来表示。我不应该强迫自己的

d.
解答可以参考:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值