算法导论第八章答案

合集下载

算法导论 第八章 线性时间排序

算法导论 第八章 线性时间排序

Decision-tree example
• Sort <a1,a2, a3>=<9,4,6>
1:2
• A decision tree can model the execution of any comparison sort: --One tree for each input size n. --View the algorithm as splitting whenever it compares two elements. -- The tree contains the comparisons along all possible instruction traces. --The running time of the algorithm = the length of the path taken. -- Worst-case running time = height of tree.
Decision-tree example
• Sort <a1,a2, a3>=<9,4,6>
1:2
2:3
1:3
123
1:3
213
2:3 4≤6
132
312
231
321
• Each internal node is labelled i:j for i,j∈{1,2,…,n} --The left subtree shows subsequent comparisons if ai≤aj --The right subtree show subsequent comparisons if ai>aj
A: B:
4 1
1 2

算法导论课程作业答案

算法导论课程作业答案

算法导论课程作业答案Introduction to AlgorithmsMassachusetts Institute of Technology 6.046J/18.410J Singapore-MIT Alliance SMA5503 Professors Erik Demaine,Lee Wee Sun,and Charles E.Leiserson Handout10Diagnostic Test SolutionsProblem1Consider the following pseudocode:R OUTINE(n)1if n=12then return13else return n+R OUTINE(n?1)(a)Give a one-sentence description of what R OUTINE(n)does.(Remember,don’t guess.) Solution:The routine gives the sum from1to n.(b)Give a precondition for the routine to work correctly.Solution:The value n must be greater than0;otherwise,the routine loops forever.(c)Give a one-sentence description of a faster implementation of the same routine. Solution:Return the value n(n+1)/2.Problem2Give a short(1–2-sentence)description of each of the following data structures:(a)FIFO queueSolution:A dynamic set where the element removed is always the one that has been in the set for the longest time.(b)Priority queueSolution:A dynamic set where each element has anassociated priority value.The element removed is the element with the highest(or lowest)priority.(c)Hash tableSolution:A dynamic set where the location of an element is computed using a function of the ele ment’s key.Problem3UsingΘ-notation,describe the worst-case running time of the best algorithm that you know for each of the following:(a)Finding an element in a sorted array.Solution:Θ(log n)(b)Finding an element in a sorted linked-list.Solution:Θ(n)(c)Inserting an element in a sorted array,once the position is found.Solution:Θ(n)(d)Inserting an element in a sorted linked-list,once the position is found.Solution:Θ(1)Problem4Describe an algorithm that locates the?rst occurrence of the largest element in a?nite list of integers,where the integers are not necessarily distinct.What is the worst-case running time of your algorithm?Solution:Idea is as follows:go through list,keeping track of the largest element found so far and its index.Update whenever necessary.Running time isΘ(n).Problem5How does the height h of a balanced binary search tree relate to the number of nodes n in the tree? Solution:h=O(lg n) Problem 6Does an undirected graph with 5vertices,each of degree 3,exist?If so,draw such a graph.If not,explain why no such graph exists.Solution:No such graph exists by the Handshaking Lemma.Every edge adds 2to the sum of the degrees.Consequently,the sum of the degrees must be even.Problem 7It is known that if a solution to Problem A exists,then a solution to Problem B exists also.(a)Professor Goldbach has just produced a 1,000-page proof that Problem A is unsolvable.If his proof turns out to be valid,can we conclude that Problem B is also unsolvable?Answer yes or no (or don’t know).Solution:No(b)Professor Wiles has just produced a 10,000-page proof that Problem B is unsolvable.If the proof turns out to be valid,can we conclude that problem A is unsolvable as well?Answer yes or no (or don’t know).Solution:YesProblem 8Consider the following statement:If 5points are placed anywhere on or inside a unit square,then there must exist two that are no more than √2/2units apart.Here are two attempts to prove this statement.Proof (a):Place 4of the points on the vertices of the square;that way they are maximally sepa-rated from one another.The 5th point must then lie within √2/2units of one of the other points,since the furthest from the corners it can be is the center,which is exactly √2/2units fromeach of the four corners.Proof (b):Partition the square into 4squares,each with a side of 1/2unit.If any two points areon or inside one of these smaller squares,the distance between these two points will be at most √2/2units.Since there are 5points and only 4squares,at least two points must fall on or inside one of the smaller squares,giving a set of points that are no more than √2/2apart.Which of the proofs are correct:(a),(b),both,or neither (or don’t know)?Solution:(b)onlyProblem9Give an inductive proof of the following statement:For every natural number n>3,we have n!>2n.Solution:Base case:True for n=4.Inductive step:Assume n!>2n.Then,multiplying both sides by(n+1),we get(n+1)n!> (n+1)2n>2?2n=2n+1.Problem10We want to line up6out of10children.Which of the following expresses the number of possible line-ups?(Circle the right answer.)(a)10!/6!(b)10!/4!(c) 106(d) 104 ·6!(e)None of the above(f)Don’t knowSolution:(b),(d)are both correctProblem11A deck of52cards is shuf?ed thoroughly.What is the probability that the4aces are all next to each other?(Circle theright answer.)(a)4!49!/52!(b)1/52!(c)4!/52!(d)4!48!/52!(e)None of the above(f)Don’t knowSolution:(a)Problem12The weather forecaster says that the probability of rain on Saturday is25%and that the probability of rain on Sunday is25%.Consider the following statement:The probability of rain during the weekend is50%.Which of the following best describes the validity of this statement?(a)If the two events(rain on Sat/rain on Sun)are independent,then we can add up the twoprobabilities,and the statement is true.Without independence,we can’t tell.(b)True,whether the two events are independent or not.(c)If the events are independent,the statement is false,because the the probability of no rainduring the weekend is9/16.If they are not independent,we can’t tell.(d)False,no matter what.(e)None of the above.(f)Don’t know.Solution:(c)Problem13A player throws darts at a target.On each trial,independentlyof the other trials,he hits the bull’s-eye with probability1/4.How many times should he throw so that his probability is75%of hitting the bull’s-eye at least once?(a)3(b)4(c)5(d)75%can’t be achieved.(e)Don’t know.Solution:(c),assuming that we want the probability to be≥0.75,not necessarily exactly0.75.Problem14Let X be an indicator random variable.Which of the following statements are true?(Circle all that apply.)(a)Pr{X=0}=Pr{X=1}=1/2(b)Pr{X=1}=E[X](c)E[X]=E[X2](d)E[X]=(E[X])2Solution:(b)and(c)only。

《算法导论(第二版)》(中文版)课后答案

《算法导论(第二版)》(中文版)课后答案

5
《算法导论(第二版) 》参考答案 do z←y 调用之前保存结果 y←INTERVAL-SEARCH-SUBTREE(y, i) 如果循环是由于y没有左子树,那我们返回y 否则我们返回z,这时意味着没有在z的左子树找到重叠区间 7 if y≠ nil[T] and i overlap int[y] 8 then return y 9 else return z 5 6 15.1-5 由 FASTEST-WAY 算法知:
15
lg n
2 lg n1 1 2cn 2 cn (n 2 ) 2 1
4.3-1 a) n2 b) n2lgn c) n3 4.3-4
2
《算法导论(第二版) 》参考答案 n2lg2n 7.1-2 (1)使用 P146 的 PARTION 函数可以得到 q=r 注意每循环一次 i 加 1,i 的初始值为 p 1 ,循环总共运行 (r 1) p 1次,最 终返回的 i 1 p 1 (r 1) p 1 1 r (2)由题目要求 q=(p+r)/2 可知,PARTITION 函数中的 i,j 变量应该在循环中同 时变化。 Partition(A, p, r) x = A[p]; i = p - 1; j = r + 1; while (TRUE) repeat j--; until A[j] <= x; repeat i++; until A[i] >= x; if (i < j) Swap(A, i, j); else return j; 7.3-2 (1)由 QuickSort 算法最坏情况分析得知:n 个元素每次都划 n-1 和 1 个,因 为是 p<r 的时候才调用,所以为Θ (n) (2)最好情况是每次都在最中间的位置分,所以递推式是: N(n)= 1+ 2*N(n/2) 不难得到:N(n) =Θ (n) 7.4-2 T(n)=2*T(n/2)+ Θ (n) 可以得到 T(n) =Θ (n lgn) 由 P46 Theorem3.1 可得:Ω (n lgn)

算法分析习题参考答案第八章

算法分析习题参考答案第八章

1.二元可满足性问题 2SAT例:给定布尔变量的一个有限集合{}n u u u U ,,,21 =及定义其上的子句{}m c c c C ,,,21 =,其中m k c k ,,2,1,2|| ==。

问:是否存在U 的一个真赋值,使得C 中所有的子句均被满足?证明: 2SAT 是P -类问题。

为叙述方便,采用数理逻辑中的“合取式”表达逻辑命题,于是∏∏==+==∧∧∧=mk k k m k k m y x c c c c C 1121)(其中j i c c ⋅表示逻辑“与”,k k y x +表示逻辑“或”,k k y x ,是某个j u 或i u 。

考虑表达式∏=+=mk k k y x C 1)(,如果有某个i i k k u u y x +=+,则在乘积式中可以去掉该子句:)(\'i i u u C C +=,可见C 与'C 的可满足性是等价的。

所以我们可以假定C 中不含有形如i i u u +的子句。

注意到此时C 中的子句个数不会超过)1(-n n 。

如果逻辑变量n u 或它的非n u 在C 的某个子句中出现,我们将C 表示成)())(()(11h n n k n n z u z u y u y u G C ++++⋅= (1) 其中G 是C 的一部分子句,而且不出现逻辑变量i u 或它的非i u 。

令∏≤≤≤≤+⋅=h j k i j i z y G C 1,1)(' (2)(2)式中不再含有变量n u 和它的非n u 。

记{}121,,,'-=n u u u U 。

如果存在U 的真赋值,使得C 满足,在'U 也一定满足。

因为如果n u 取真值,则所有的j z 必然取真值;n u 取假值,所有的i y 必然都取真值,不管那中情况,'C 的乘积部分必然取真值。

反之,假设存在'U 的真赋值,使得'C 满足。

若有某个i y 取假值,则所有的j z 必然取真值,此时令n u 取真值,得到U 的真赋值,使得C 满足。

高等数学课后答案 第八章 习题详细解答

高等数学课后答案 第八章 习题详细解答

习 题 8-11.设有一个面薄板(不计其厚度),占有xOy 面上的闭区域D ,薄板上分布有面密度为(,)x y μμ=的电荷,且(,)x y μ在D 上连续,试用二重积分表达该板上的全部电荷Q .解 用一组曲线将D 分成n 个小闭区域i σ∆,其面积也记为(1,2,,)i i n σ∆= .任取一点(,)i i i ξησ∈∆,则i σ∆上分布的电量(,)i i i Q μξησ∆≈∆.通过求和、取极限,便得到该板上的全部电荷为1lim (,)(,)d ,ni i i i DQ x y λμξησμσ→==∆=∑⎰⎰其中1max{i i nλσ≤≤=∆的直径}.2. 设12231()d D I x y σ=+⎰⎰其中1{(,)11,22}D x y x y =-≤≤-≤≤;又22232()d D I x y σ=+⎰⎰其中2{(,)01,02}D x y x y =≤≤≤≤.试利用二重积分的几何意义说明1I 与2I 之间的关系.解 由二重积分的几何意义知,1I 表示底为1D 、顶为曲面223()z x y =+的曲顶柱体1Ω的体积;2I 表示底为2D 、顶为曲面223()z x y =+的曲顶柱体2Ω的体积.由于位于1D 上方的曲面223()z x y =+关于yOz 面和zOx 面均对称,故yOz 面和zOx 面将1Ω分成四个等积的部分,其中位于第一卦限的部分即为2Ω.由此可知124I I =.3. 利用二重积分定义证明: (1) d ()DD σσσ=⎰⎰其中为的面积;(2) (,)d (,)d ()DDkf x y k f x y k σσ=⎰⎰⎰⎰其中为常数;(3)12(,)d (,)d (,)d ,DD D f x y f x y f x y σσσ=+⎰⎰⎰⎰⎰⎰其中12D D D= ,1D 、2D 为两个无公共内点的闭区域.证 (1) 由于被积函数(,)1f x y ≡,故由二重积分定义得11d lim (,)lim lim .nniiii i i Df λλλσξησσσσ→→→===∆=∆==∑∑⎰⎰(2) 011(,)d lim (,)lim (,)(,)d .nni i i i i i i i DDkf x y kf k f k f x y λλσξησξησσ→→===∆=∆=∑∑⎰⎰⎰⎰(3) 因为函数(,)f x y 在闭区域D 上可积,故不论把D 怎样分割,积分和的极限总是不变的,因此在分割D 时,可以使1D 和2D 的公共边界永远是一条分割线。

算法导论 第八章答案

算法导论 第八章答案

{ if(m >n)
cout<<"区间输入不对"<<endl; else { if(n <0) else if(m <=0&& n <= k) else if(n > k && m >0) else if(n > k && m <=0) else
cout<<"个数为"<<0<<endl; cout<<"个数为"<<c[n]<<endl; cout<<"个数为"<<c[k] - c[m -1]<<endl;
cout<<"个数为"<<c[k]<<endl; cout<<"个数为"<<c[n] - c[m -1]<<endl;
}}
return 0;
}
void counting_sort(int*&a, int length, int k, int*&b, int*&c)
{ for(int i =0; i < k +1; i++)
const int k =2504; int* c =new int[k +1];
counting_sort(a, LEN, k, b, c); //这里需要注释掉
//for(int i = 0; i < LEN; i++)
// cout<<b[i]<<endl;

ch8参考答案

ch8参考答案
千里之行,始于足下。
算法作业第八章参考答案
8:23
KRUSKAL 算法找最小生成树
解:首先将图的边按照权值排好序。
每次贪婪选择最小边,检测是否产生回路,并将不形成
回路的边加入生成树的局部解。过程演示如下
1
6
3
4
5
27
37
1
9
3
2
2
4
6
6
1
6
3
4
5
27
37
1
9
3
2
2
4
6
6
第 1 页/共 5 页
千里之行,始于足下。
第 3 页/共 5 页
千里之行,始于足下。
算法作业第八章参考答案
8.31 用 Huffuman 算法构造编码树。 abcdef 7 5 3 2 12 9
构造过程(红色为叶节点,绿色由红色扩展而来):
5
2
3
5
7
d
c
b
a
10
5
5
9 12
f
e
2
3
7
9 12
第 4 页/共 5 页
千里之行,始于足下。
算法作业第八章参考答案
10
5
5
16
2
3
7
9 12
22
10
12
5
5
1
2
3
7
9
参考编码:a : 10
b : 001 c : 0001
e : 01
f : 11
d : 0000
第 5 页/共 5 页
算法作业第八章参考答案
1
6
3

中科大算法导论作业标准标准答案

中科大算法导论作业标准标准答案

第8次作业答案16.1-116.1-2543316.3-416.2-5参考答案:16.4-1证明中要三点:1.有穷非空集合2.遗传性3.交换性第10次作业参考答案16.5-1题目表格:解法1:使用引理16.12性质(2),按wi单调递减顺序逐次将任务添加至Nt(A),每次添加一个元素后,进行计算,{计算方法:Nt(A)中有i个任务时计算N0 (A),…,Ni(A),其中如果存在Nj (A)>j,则表示最近添加地元素是需要放弃地,从集合中删除};最后将未放弃地元素按di递增排序,放弃地任务放在所有未放弃任务后面,放弃任务集合内部排序可随意.解法2:设所有n个时间空位都是空地.然后按罚款地单调递减顺序对各个子任务逐个作贪心选择.在考虑任务j时,如果有一个恰处于或前于dj地时间空位仍空着,则将任务j赋与最近地这样地空位,并填入; 如果不存在这样地空位,表示放弃.答案(a1,a2是放弃地):<a5, a4, a6, a3, a7,a1, a2>or <a5, a4, a6, a3, a7,a2, a1>划线部分按上表di递增地顺序排即可,答案不唯一16.5-2(直接给个计算例子说地不清不楚地请扣分)题目:本题地意思是在O(|A|)时间内确定性质2(性质2:对t=0,1,2,…,n,有Nt(A)<=t,Nt(A)表示A中期限不超过t地任务个数)是否成立.解答示例:思想:建立数组a[n],a[i]表示截至时间为i地任务个数,对0=<i<n,如果出现a[0]+a[1]+…+a[i]>i,则说明A不独立,否则A独立.伪代码:int temp=0;for(i=0;i<n;i++) a[i]=0; ******O(n)=O(|A|)for(i=0;i<n;i++) a[di]++; ******O(n)=O(|A|)for(i=0;i<n;i++) ******O(n)=O(|A|) {temp+=a[i];//temp就是a[0]+a[1]+…+a[i]if(temp>i)//Ni(A)>iA不独立;}17.1-1(这题有歧义,不扣分)a) 如果Stack Operations包括Push Pop MultiPush,答案是可以保持,解释和书上地Push Pop MultiPop差不多.b) 如果是Stack Operations包括Push Pop MultiPush MultiPop,答案就是不可以保持,因为MultiPush,MultiPop交替地话,平均就是O(K).17.1-2本题目只要证明可能性,只要说明一种情况下结论成立即可17.2-1第11次作业参考答案17.3-1题目:答案:备注:最后一句话展开:采用新地势函数后对i 个操作地平摊代价:)1()())1(())(()()(1''^'-Φ-Φ+=--Φ--Φ+=Φ-Φ+=-Di Di c k Di k Di c D D c c i i i i i i17.3-2题目:答案:第一步:此题关键是定义势能函数Φ,不管定义成什么首先要满足两个条件 对所有操作i ,)(Di Φ>=0且)(Di Φ>=)(0D Φ比如令k j+=2i ,j,k 均为整数且取尽可能大,设势能函数)(Di Φ=2k;第二步:求平摊代价,公式是)1()(^-Φ-Φ+=Di Di c c i i 按上面设置地势函数示例:当k=0,^i c =…=2当k !=0,^i c =…=3 显然,平摊代价为O(1)17.3-4题目:答案:结合课本p249,p250页对栈操作地分析很容易有下面结果17.4-3题目:答案:αα=(第i次循环之后地表中地entry 假设第i个操作是TABLE_DELETE, 考虑装载因子:inum size数)/(第i次循环后地表地大小)=/i i第12 次参考答案19.1.1题目:答案:如果x不是根,则degree[sibling[x]]=degree[child[x]]=degree[x]-1如果x是根,则sibling为二项堆中下一个二项树地根,因为二项堆中根链是按根地度数递增排序,因此degree[sibling[x]]>degree[x]19.1.2题目:答案:如果x是p[x]地最左子节点,则p[x]为根地子树由两个相同地二项树合并而成,以x为根地子树就是其中一个二项树,另一个以p[x]为根,所以degree[p[x]]=degree[x]+1;如果x不是p[x]地最左子节点,假设x是p[x]地子节点中自左至右地第i个孩子,则去掉p[x]前i-1个孩子,恰好转换成第一种情况,因而degree[p[x]]=degree[x]+1+(i-1)=degree[x]+i;综上,degree[p[x]]>degree[x]19.2.2题目:题目:19.2.519.2.6第13次作业参考答案20.2-1题目:解答:20.2-3 题目:解答:20.3-1 题目:答案:20.3-2 题目:答案:第14次作业参考答案这一次请大家自己看书处理版权申明本文部分内容,包括文字、图片、以及设计等在网上搜集整理.版权为个人所有This article includes some parts, including text, pictures, and design. Copyright is personal ownership.6ewMy。

算法导论(第二版)习题答案(英文版)

算法导论(第二版)习题答案(英文版)

Last update: December 9, 2002
1.2 − 2 Insertion sort beats merge sort when 8n2 < 64n lg n, ⇒ n < 8 lg n, ⇒ 2n/8 < n. This is true for 2 n 43 (found by using a calculator). Rewrite merge sort to use insertion sort for input of size 43 or less in order to improve the running time. 1−1 We assume that all months are 30 days and all years are 365.
n
Θ
i=1
i
= Θ(n2 )
This holds for both the best- and worst-case running time. 2.2 − 3 Given that each element is equally likely to be the one searched for and the element searched for is present in the array, a linear search will on the average have to search through half the elements. This is because half the time the wanted element will be in the first half and half the time it will be in the second half. Both the worst-case and average-case of L INEAR -S EARCH is Θ(n). 3

麻省理工大学算法导论lecture08

麻省理工大学算法导论lecture08

of hash functions. Suppose h is used to hash n arbitrary keys into the m slotsen key x, we have E[#collisions with x] < n/m.
2001 by Charles E. Leiserson Introduction to Algorithms Day 12 L8.2
Universal hashing
Definition. Let U be a universe of keys, and let H be a finite collection of hash functions, each mapping U to {0, 1, …, m–1}. We say H is universal if for all x, y ∈ U, where x ≠ y, we have |{h ∈ H : h(x) = h(y)}| = |H|/m. That is, the chance of a collision between x and y is 1/m if we choose h randomly from H.
i =0 i =1
r
(mod m)
or r a0 ( x0 y0 ) + ∑ ai ( xi yi ) ≡ 0 which implies that a0 ( x0 y0 ) ≡ ∑ ai ( xi yi )
i =1
2001 by Charles E. Leiserson Introduction to Algorithms
y∈T {x}
∑ cxy .
2001 by Charles E. Leiserson

算法导论 答案

算法导论 答案

算法导论答案算法导论是计算机科学领域中一门重要的课程,其目的是介绍和探讨算法的基本原理和设计方法。

本文将从几个不同角度来讨论算法导论的主题,包括算法的定义、算法分析、算法设计等,并提供一些常见的算法应用示例。

一、算法的定义与特点算法是一个用于完成特定任务的有限一系列指令或规则的集合。

算法的设计应具备以下几个特点:确定性、有穷性、可行性和输入/输出。

1. 确定性:对于给定的输入,算法的每个步骤都必须是明确且唯一的。

不同的输入应得到不同的输出。

2. 有穷性:算法必须经过有限的步骤后能终止。

这是因为计算机程序需要在有限时间内完成,而无限循环的算法则无法给出结果。

3. 可行性:算法的每个步骤都应该是可行执行的,即能够在可接受的时间内完成。

4. 输入/输出:算法应该具有输入和输出,即根据给定的输入,通过算法能够得到相应的输出。

二、算法的分析与效率评估算法的分析与效率评估是算法导论的重要内容。

在设计和选择算法时,需要考虑到其执行时间和内存需求等方面。

1. 时间复杂度:用于衡量算法所需执行的时间。

常见的时间复杂度表示方法有大O记法,例如O(n)、O(nlogn)等。

时间复杂度越低,算法执行效率越高。

2. 空间复杂度:用于衡量算法所需的内存空间。

空间复杂度的衡量单位通常是字节或比特。

与时间复杂度一样,空间复杂度越低,算法的内存需求越小。

三、算法的设计方法算法设计是算法导论的核心内容之一,主要包括贪心算法、分治算法、动态规划算法等。

1. 贪心算法:贪心算法是一种基于每一步局部最优解的策略来解决问题的算法。

它常应用于问题的最优解为局部最优解的情况,但不一定能得到全局最优解。

2. 分治算法:分治算法是一种将问题划分为多个相互独立的子问题,再合并子问题的解来得到整体解的算法。

适合解决规模较大且问题可被划分的情况。

3. 动态规划算法:动态规划算法是一种将复杂问题拆分为更小、更简单的子问题来解决的算法。

它利用了子问题的重叠性质,将子问题的解保存起来,避免了重复计算。

算法导论(第二版)课后习题解答

算法导论(第二版)课后习题解答
n
Θ
i=1
i
= Θ(n2 )
This holds for both the best- and worst-case running time. 2.2 − 3 Given that each element is equally likely to be the one searched for and the element searched for is present in the array, a linear search will on the average have to search through half the elements. This is because half the time the wanted element will be in the first half and half the time it will be in the second half. Both the worst-case and average-case of L INEAR -S EARCH is Θ(n). 3
Solutions for Introduction to algorithms second edition
Philip Bille
The author of this document takes absolutely no responsibility for the contents. This is merely a vague suggestion to a solution to some of the exercises posed in the book Introduction to algorithms by Cormen, Leiserson and Rivest. It is very likely that there are many errors and that the solutions are wrong. If you have found an error, have a better solution or wish to contribute in some constructive way please send a message to beetle@it.dk. It is important that you try hard to solve the exercises on your own. Use this document only as a last resort or to check if your instructor got it all wrong. Please note that the document is under construction and is updated only sporadically. Have fun with your algorithms. Best regards, Philip Bille

算法导论中文版答案

算法导论中文版答案

24.2-3
24.2-4
24.3-1 见图 24-6 24.3-2
24.3-3
24.3-4 24.3-5 24.3-6
24.3-7
24.3-8 这种情况下不会破坏已经更新的点的距离。 24.4**** 24.5****
25.1-1 见图 25-1 25.1-2 为了保证递归定义式 25.2 的正确性 25.1-3
8.3-3 8.3-4
8.3-5(*) 8.4-1 见图 8-4 8.4-2
8.4-3 3/2,1/2 8.4-4(*) 8.4-5(*)
9.1-1
9.1-2 9.2-1 9.3-1
第九章
9.3-2 9.3-3
9.3-4 9.3-5
9.3-6 9.3-7
9.3-8
9.3-9
15.1-1
6.4-4
6.4-5
6.5-1 据图 6-5 6.5-2
6.5-3 6.5-4 6.5-5
6.5-6 6.5-7
6.5-8
7.1-1 见图 7-1 7.1-2
7.1-3 7.1-4 7.2-1 7.2-2
7.2-3 7.2-4 7.2-5
第七章
7.2-6 7.3-1
7.3-2
7.4-1 7.4-2
5.3-6
6.1-1 6.1-2 6.1-3 6.1-4 6.1-5 6.1-6
第6章
6.1-7
6.2-1 见图 6-2 6.2-2
6.2-3
6.2-4
6.2-5 对以 i 为根结点的子树上每个点用循环语句实现 6.2-6
6.3-1
见图 6-3 6.3-2
6.3-3
6.4-1 见图 6-4 6.4-2 HEAPSORT 仍然正确,因为每次循环的过程中还是会运行 MAX-HEAP 的过程。 6.4-3

算法导论中文版答案

算法导论中文版答案
len=j;//更新 len
} cout<<len<<endl; } return 0; } 15.5-1
15.5-2 15.5-3
15.5-4
16.1-1
第 16 章
16.1-2 16.1-3
16.1-4 16.2-1 16.2-2
16.2-3
16.2-4
16.2-5 16.2-6
16.2-7
25.3-6
5.3-6
6.1-1 6.1-2 6.1-3 6.1-4 6.1-5 6.1-6
第6章
6.1-7
6.2-1 见图 6-2 6.2-2
6.2-3
6.2-4
6.2-5 对以 i 为根结点的子树上每个点用循环语句实现 6.2-6
6.3-1
见图 6-3 6.3-2
6.3-3
6.4-1 见图 6-4 6.4-2 HEAPSORT 仍然正确,因为每次循环的过程中还是会运行 MAX-HEAP 的过程。 6.4-3
6.4-4
6.4-5
6.5-1 据图 6-5 6.5-2
6.5-3 6.5-4 6.5-5
6.5-6 6.5-7
6.5-8
7.1-1 见图 7-1 7.1-2
7.1-3 7.1-4 7.2-1 7.2-2
7.2-3 7.2-4 7.2-5
第七章
7.2-6 7.3-1
7.3-2
7.4-1 7.4-2
16.3-1 16.3-2
16.3-3 16.3-4
16.3-5
16.3-6 那就推广到树的结点有三个孩子结点,证明过程同引理 16.3 的证明。 16.3-7 16.3-8
第 24 章
24.1-1 同源顶点 s 的运行过程,见图 24-4 24.1-2

数据结构与算法第8章答案

数据结构与算法第8章答案

第8 章排序技术课后习题讲解1. 填空题⑴排序的主要目的是为了以后对已排序的数据元素进行()。

【解答】查找【分析】对已排序的记录序列进行查找通常能提高查找效率。

⑵对n个元素进行起泡排序,在()情况下比较的次数最少,其比较次数为()。

在()情况下比较次数最多,其比较次数为()。

【解答】正序,n-1,反序,n(n-1)/2⑶对一组记录(54, 38, 96, 23, 15, 72, 60, 45, 83)进行直接插入排序,当把第7个记录60插入到有序表时,为寻找插入位置需比较()次。

【解答】3【分析】当把第7个记录60插入到有序表时,该有序表中有2个记录大于60。

⑷对一组记录(54, 38, 96, 23, 15, 72, 60, 45, 83)进行快速排序,在递归调用中使用的栈所能达到的最大深度为()。

【解答】3⑸对n个待排序记录序列进行快速排序,所需要的最好时间是(),最坏时间是()。

【解答】O(nlog2n),O(n2)⑹利用简单选择排序对n个记录进行排序,最坏情况下,记录交换的次数为()。

【解答】n-1⑺如果要将序列(50,16,23,68,94,70,73)建成堆,只需把16与()交换。

【解答】50⑻对于键值序列(12,13,11,18,60,15,7,18,25,100),用筛选法建堆,必须从键值为()的结点开始。

【解答】60【分析】60是该键值序列对应的完全二叉树中最后一个分支结点。

2. 选择题⑴下述排序方法中,比较次数与待排序记录的初始状态无关的是()。

A插入排序和快速排序B归并排序和快速排序C选择排序和归并排序D插入排序和归并排序【解答】C【分析】选择排序在最好、最坏、平均情况下的时间性能均为O(n2),归并排序在最好、最坏、平均情况下的时间性能均为O(nlog2n)。

⑵下列序列中,()是执行第一趟快速排序的结果。

A [da,ax,eb,de,bb] ff [ha,gc]B [cd,eb,ax,da] ff [ha,gc,bb]C [gc,ax,eb,cd,bb] ff [da,ha]D [ax,bb,cd,da] ff [eb,gc,ha]【解答】A【分析】此题需要按字典序比较,前半区间中的所有元素都应小于ff,后半区间中的所有元素都应大于ff。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
相关文档
最新文档