算法导论第三版第二章答案

第二章

1-1 1-2 1-3 1-4

2-1 2-2 2-3 2-4

3-1 3-2 3-3 3-4 3-5 3-6 3-7

1-1

Using figure 2-2 as a model, illustrate the operations of Insertion-Sort on the array A=⟨31,41,59,26,41,58⟩.
以图2-2为模型,说明Insertion-Sort在数组A=⟨31,41,59,26,41,58⟩上的执行情况

在这里插入图片描述

1-2

Rewrite the Insertion-Sort procedure to sort into nonincreasing instead of nondecreasing order.
重写Insertion-Sort,使之按非升序排序

for j = 2 to A.length
    key = A[j]
    i = j - 1
    while i > 0 and A[i] < key
        A[i + 1] = A[i]
        i = i - 1
    A[i + 1] = key

1-3

Consider the searching problem:
Input: A sequence of n n n numbers A=⟨a1,a2,…,an⟩ and a value ν.
Output: And index i such that ν=A[i] or the special value NIL if ν does not appear in A.
Write the pseudocode for linear search, which scans through the sequence, looking for ν. Using a loop invariant, prove that your algorithm is correct. Make sure that your loop invariant fulfills the three necessary properties.
考虑以下查找问题:
输入:n个数的一个序列A=⟨a1,a2,…,an⟩和一个值v
输出:下标i使得ν=A[i]或者当v不在A中出现时,v为特殊值NIL
写出线性查找的伪代码,他扫描整个序列来查找v。使用一个循环不变式来证明你的算法是正确的。确保你的循环不变式满足三条必要的性质

linear_search(A,v)
for j=1 to A.length
	if A[j]==v
		return j
return NIL

循环不变式是A[1,j-1]中的所有元素都不同于v
初始化:A[1,j-1]为空,成立
保持:对于接下来的每一步我们都有A[1,j-1]中所有元素不同于v,因为一旦A[j]与v相同,我们就返回j
终止:循环终止条件是j>A.length,因此所以A中的数都比较过了,而v与它们都不同,所以返回NIL

1-4

Consider the problem of adding two n-bit binary integers, stored in two n-element arrays A and B. The sum of the two integers should be stored in binary form in an (n+1)-element array C. State the problem formally and write pseudocode for adding the two integers.
考虑把两个n进制整数加起来的问题,这两个整数分别存储在两个n元数组A和B中。把这两个整数的和应按二进制形式存储在一个(n+1)元数组C中。请给出该问题的形式化描述,并写出伪代码

输入:两个n元二进制数组A<a1,a2…an>,B<b1,b2…bn>
输出:n+1元二进制数组C,C满足C=A+B

add(A, B)
C = new[A.length - 1]
carry = 0
for j = 1 to A.length
	C[j] = (A[j] + B[j]) + carry) % 2
	carry = (A[j] + B[j]) + carry) / 2
return C

2-1

Express the function n3/1000−100n2−100n+3 in terms of Θ-notation
用Θ记号表示函数n3/1000-100n2-100n+3

Θ(n3)

2-2

Consider sorting n3 numbers in an array A by first finding the smallest element of A and exchanging it with the element in A[1]. Then find the second smallest element of A, and exchange it with A[2]. Continue in this manner for the first n−1 elements of A. Write pseudocode for this algorithm, which is known as selection sort. What loop invariants does this algorithm maintain? Why does it need to run for only the first n−1 elements, rather than for all n elements? Give the best-case and the worst-case running times of selection sort in Θ-notation.
考虑排序存储在数组A中的n个数:首先找出A中的最小元素并将其与A[1]中的元素进行交换。接着,找出A中的次最小元素并将其与A[2]中的元素进行交换。对A中前n-1个元素按该方式继续。该算法称为选择算法,写出其伪代码。该算法维持的循环不变式是什么?为什么它只需要对前n-1个元素,而不是对所有n个元素运行?用Θ记号给出选择排序的最好情况与最坏情况运行时间。

select_sort(A)
for i in 1 to A.length-1
	min=i
	for j in i+1 to A.length
		if A[j]<A[min]
			min=j
	temp=A[i]
	A[i]=A[min]
	A[min]=temp
  • 循环不变式是A[1,i-1]包含前i-1个最小的元素,且已经排好序
  • 为什么是n-1:因为前n-1个数排好序后,最后一个数一定是最大的,所以不用排它了
  • 最好情况是(c1+c2+c6+c7+c8)(n-1)+(c3+c4)(n-1+n-2+…+1)=(c1+c2+c6+c7+c8)(n-1)+(c3+c4)(n-1)n/2
    最坏情况是(c1+c2+c6+c7+c8)(n-1)+(c3+c4+c5)(n-1)n/2
    显然都是Θ(n2
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
算法导论(原书第2)》一书深入浅出,全面地介绍了计算机算法。对每一个算法的分析既易于理解又十分有趣,并保持了数学严谨性。本书的设计目标全面,适用于多种用途。涵盖的内容有:算法在计算中的作用,概率分析和随机算法的介绍。本书专门讨论了线性规划,介绍了动态规划的两个应用,随机化和线性规划技术的近似算法等,还有有关递归求解、快速排序中用到的划分方法与期望线性时间顺序统计算法,以及对贪心算法元素的讨论。本书还介绍了对强连通子图算法正确性的证明,对哈密顿回路和子集求和问题的NP完全性的证明等内容。全书提供了900多个练习题和思考题以及叙述较为详细的实例研究。   《算法导论(原书第2)》一书内容丰富,对本科生的数据结构课程和研究生的算法课程都是很实用的教材。本书在读者的职业生涯中,也是一本案头的数学参考书或工程实践手册。   在有关算法的书中,有一些叙述非常严谨,但不够全面,另一些涉及了大量的题材,但又缺乏严谨性。《算法导论》将严谨性和全面性融为一体。   《算法导论(原书第2)》一书深入讨论各类算法,并着力使这些算法的设计和分析能为各个层次的读者接受。各章自成体系,可以作为独立的学习单元。算法以英语和伪代码的形式描述,具备初步程序设计经验的人就能看懂。说明和解释力求浅显易懂,不失深度和数学严谨性。   《算法导论(原书第2)》一书自第1以来,已经成为世界范围内广泛使用的大学教材和专业人员的标准参考书。第2增加了论述算法作用、概率分析与随机算法、线性规划等几章。同时,对第1的几乎每一节都作了大量的修订。一项巧妙而又重要的修改是提前引入循环不变式,并在全书中用来证明算法的正确性。在不改变数学和分析重点的前提下,作者将许多数学基础知识从第一部分移到了附录中,并在开始部分加入了一些富有诱导性的题材。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值