算法导论第二十章答案
算法导论课程作业答案

算法导论课程作业答案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。
大学教材课后习题答案免费下载链接下部

大学教材课后习题答案免费下载链接(上中下)190-290本资料由上网购返利网分享汽车理论习题答案(考研_作业).pdf→→/s/1zobam汽车理论第五版_课后习题答案(正确).pdf→→/s/1o67DaHk波动习题答案.pdf→→/s/1pJDGFyj泵与风机课后习题答案.pdf→→/s/1gdBph3H 流体力学习题解答李晓燕吴邦喜.pdf→→/s/1qWM2gAo液压与气压传动习题答案.pdf→→/s/1bnksUmV物理化学第五版习题解答(上下册).pdf→→/s/1sjvvFPj物理学教程第二版马文蔚下册课后答案完整版_cropped.pdf→→/s/1sj98Mct物理学第五版上册习题答案.pdf→→/s/1jG1F9NS王勖成《有限单元法》1-5章课后习题答案.pdf→→/s/1nt8vc3B理论力学教程_第三版_周衍柏_课后习题答案_总汇(1).pdf→→理论力学教程_第三版_周衍柏_课后习题答案_总汇.pdf→→/s/1eQABmxW电力系统分析课后习题答案.pdf→→/s/1bngpktD电动力学习题答案chapter5.pdf→→/s/1pJ7AZ5x电子商务法律与法规综合复习题与答案.pdf→→/s/1c0nEFUo电子测量技术基础课后习题答案上1,2,5,6,7,8.pdf→→/s/1hq3f7Is电子线路习题答案梁明理版.pdf→→/s/1bn5rEIr电工学简明教程(第二版)学习辅导与习题解答.pdf→→/s/1mgHQ6xi电机与拖动基础第三版李发海答案(全).pdf→→/s/1dD25KyP电气测试技术第三版_课后习题答案%28林德杰%29.pdf→→/s/1jGwVRE2电磁场与电磁波习题答案 (6).pdf→→/s/1bnrK3pX电磁场与电磁波习题答案 (7).pdf→→电磁场与电磁波习题答案 (8).pdf→→/s/1mgLUqCC电磁场与电磁波习题答案 .pdf→→/s/1hqsqmX2电磁场与电磁波习题答案2.pdf→→/s/1pJDGF0n电路(第五版)_课后习题答案(全)].邱关源_罗先觉_高等教育出版社.pdf→→/s/1sjtZPBR电路与电子学模拟复习题答案.pdf→→/s/1pJvzN6r电路第五版课后习题答案上册.pdf→→/s/1jG3bA30电路第五版课后习题答案下册.pdf→→/s/1jGn22Ke病理学试题库及答案.pdf→→/s/1hqp80wW 看完包过江苏材料员习题和答案——本人亲自考过.pdf→→/s/1qWyL8IS离散数学课后习题答案_屈婉玲(高等教育出版社).pdf→→/s/1gdghe9t算法导论课后习题与思考题答案合集.pdf→→/s/1pJqaiQf粤教版高中物理必修一课后习题答案(1~4章).pdf→→/s/1gdoU5qF线性代数习题册答案(理).pdf→→/s/1dD5n9ZV线性代数习题解答.pdf→→/s/1i3JY7PV线性规划习题答案.pdf→→/s/1hqiMQ00组合数学课后习题答案.pdf→→/s/1hqwuajE 组合逻辑电路课后习题答案.pdf→→/s/1bnnaNwF经典国外教材atkins物理化学第七版课后习题答案.pdf→→/s/1i39jobj经济学原理习题解答第五版%28微观、宏观).pdf→→/s/1sjm8rKT经济应用数学(三)概率论与数理统计修订版(袁荫棠编)习题答案.pdf →→/s/1ntsgiBR结构力学上龙驭求包世华课后习题答案.pdf→→/s/1eQiQVAU结构力学答案(全).pdf→→/s/1qW2Pud2结构力学课后习题答案.pdf→→/s/1kTKA1sN 结构力学龙驭球习题解答(ch2~ch3).pdf→→/s/1eQ676WM结构化学基础第四版习题答案.pdf→→/s/1jGkKEDw罗默《高级宏观经济学》课后习题答案中文版(金圣才主编).pdf→→/s/1c0h9fxe考研数学一历年真题答案(2002-2011).pdf→→/s/1qWoCltU胡寿松《自动控制原理》(第四版)课件_习题答案.pdf→→/s/1o6qcjqy自动控制原理习题解答(第二版)(余成波_张莲_胡.pdf→→/s/1pJsHZ0R自动控制原理习题解答.pdf→→/s/1bn3VJPH 船舶结构力学—课后习题答案.pdf→→/s/1lBC6西安理工大学_供电技术习题答案_机械工业出版社_第四版.pdf→→/s/1kTyVJSz西方经济学简明教程(第七版)习题参考答案.pdf→→/s/1bnmuORd计算机组成原理习题答案.pdf→→/s/1pJAYQQv计算机组成原理课后习题答案.pdf→→/s/1o68tfcA计算机编译原理课后习题答案第三版张幸儿.pdf→→/s/1eQikYlW计算机网络(第4版)习题答案(中文版).pdf→→/s/1dDcNuTj计算机网络(第五版)习题答案_谢希仁.pdf→→/s/1dDmWLJb计算机网络第四版习题答案(第五版上的很多题在这都能找到答案).pdf→→/s/1qWnqs0G计量经济学习题与解答于俊年主编对外经济贸易大学出版社.pdf→→/s/1i3hyUTv证券交易习题大全与答案.pdf→→/s/1o6NSibo证券投资基金章节习题及答案.pdf→→/s/1mgFiKWS课后习题答案(叶见曙主编结构设计原理1-9章).pdf→→/s/1pJ6Vfp5贾俊平_统计学_第四版_习题答案.pdf→→/s/1jG9hn0M软件工程导论(第五版)_(张海藩_着)_清华大学出版社_课后习题答案.pdf→→/s/1kT8zkz1软件工程导论-第五版_课后习题答案%28清华大学出版社%29张海藩著.pdf→→/s/1qWtvU9m软件工程导论_张海藩_第五版课后习题答案.pdf→→/s/1mgK802G运筹学基础及应用第五版胡运权主编课后练习答案.pdf→→/s/1o6K4t8e近代物理复习题答案.pdf→→/s/1bnzmbTl 通信原理_李晓峰_课后习题答案.pdf→→/s/1pJ2lyXX通信原理习题及答案(第六版)_樊昌信_曹丽娜_编著__国防工业出版社.pdf→→/s/1jGDQUJ8通信电子线路习题解答(严国萍版).pdf→→/s/1ntjs7hf逻辑代数基础课后习题答案.pdf→→/s/1o6ufVGe重点推荐---《数值分析》课后习题答案.pdf→→/s/1eQ456QQ量子力学习题解答-第3章.pdf→→/s/1sjoAeID量子力学练习题答案.pdf→→/s/1eQqsylG 金属材料学习题答案(曹志强老师的).pdf→→/s/1gdwvZFl钢结构基础(第二版)课后习题答案.pdf→→/s/1sjlSrut铁道社单片机习题参考答案.pdf→→/s/1o6qIbSA随机过程习题解答.pdf→→/s/1o6G0618集成电子技术基础教程(上)习题解答.pdf→→/s/1i3mO3fn雷达原理习题解答1.pdf→→/s/13XQ6U高一数学期末复习题及答案.pdf→→/s/1ntlu7g9高中物理必修1、必修2课后习题答案(人教版).pdf→→/s/1jGFSIME高二数列复习题答案.pdf→→/s/1o6lY2nC 高电压技术习题与答案.pdf→→/s/1sjEY8pB 高等代数北大第三版习题全解王萼芳石生明修订.pdf→→/s/1gdn87ZX高等代数_北大第三版_习题答案.pdf.pdf→→/s/1o6lYx6M高等土力学(李广信)1-5章部分习题答案(最新版).pdf→→/s/1jGC4XGa高等教育出版社离散数学课后习题完整答案.pdf→→/s/1dDy2aTb高考数学填空题解答策略.pdf→→/s/1vQmF8 高频电子线路习题答案_张肃文__第五版.pdf→→/s/1nt7Ff1v高鸿业版宏观经济学课后习题答案_%28第十二章到二十三章%29第五版全.pdf→→/s/1sjBAEq5王镜岩生物化学课后习题答案.pdf→→/s/1hqn649e《国际金融学》习题与答案→→/s/1eQ1NKe2 黄达《金融学》精编版(第二版)课后习题答案→→/s/1pJFI9j92014年江南大学微生物学教程(第二版周德庆)考研资料及历年真题答案→→/s/1hq7Pxfm中南大学土木工程材料课后习题及答案→→/s/1o64psNo本资料分上,中,下部三份,欢迎查阅下载建筑装修用花岗岩石材友情赞助。
算法导论 答案 (2)

算法导论答案算法导论概述《算法导论》是一本经典的计算机科学教材,由Thomas H. Cormen、Charles E. Leiserson、Ronald L. Rivest和Clifford Stein合著。
这本书详细介绍了算法的设计、分析和实现,并涵盖了算法导论领域的许多重要概念和技术。
本文将为你提供一些关于《算法导论》中一些常见问题的答案。
1. 什么是算法?算法是一系列明确定义的步骤,用于解决特定问题或完成特定任务。
它可以是一个计算过程、一个程序或一个有限的操作序列。
算法通常用于计算和数据处理领域,是计算机科学的核心概念。
2. 为什么学习算法很重要?学习算法的重要性体现在以下几个方面:•提高问题解决能力:算法是解决问题的有效工具。
学习算法可以帮助我们思考和理解问题,并设计出相应的解决方案。
•优化计算性能:算法的设计和分析可以帮助我们提高计算的效率和性能。
合适的算法可以在短时间内处理大规模数据集和复杂计算任务。
•促进技术创新:算法是许多技术和应用的基石,包括人工智能、机器学习、数据挖掘等。
学习算法可以为我们打开更多的研究和创新机会。
3. 《算法导论》提供了哪些内容?《算法导论》这本书详细介绍了算法的基本概念和设计技巧,并提供了许多典型算法的实现和分析。
以下是该书的一些主要内容:•算法分析:对算法进行时间复杂度和空间复杂度的理论分析,帮助我们评估算法的效率和性能。
•排序和查找算法:介绍了各种排序算法(如插入排序、归并排序、快速排序)和查找算法(如二分查找、哈希表)。
•图算法:讨论了图的表示方法和图搜索算法(如深度优先搜索、广度优先搜索)以及最短路径算法(如Dijkstra算法)等。
•动态规划和贪心算法:介绍了动态规划和贪心算法的原理和应用,用于解决具有最优子结构性质的问题。
•分治算法和递归思想:讲解了分治算法的基本原理,并提供了许多使用递归思想解决问题的例子。
•NP完全问题:探讨了NP完全问题的性质和求解方法,引导了读者进入计算复杂性理论的领域。
算法导论习题答案 (5)

Three-hole punch your paper on submissions. You will often be called upon to “give an algorithm” to solve a certain problem. Your write-up should take the form of a short essay. A topic paragraph should summarize the problem you are solving and what your results are. The body of the essay should provide the following:
(a) Argue that this problem exhibits optimal substructure.
Solution: First, notice that linecost(i, j) is defined to be � if the words i through j do not fit on a line to guarantee that no lines in the optimal solution overflow. (This relies on the assumption that the length of each word is not more than M .) Second, notice that linecost(i, j) is defined to be 0 when j = n, where n is the total number of words; only the actual last line has zero cost, not the recursive last lines of subprob lems, which, since they are not the last line overall, have the same cost formula as any other line.
《算法导论(第二版)》(中文版)课后答案

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)
中科大算法导论作业标准标准答案

第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。
算法导论参考 答案

第二章算法入门由于时间问题有些问题没有写的很仔细,而且估计这里会存在不少不恰当之处。
另,思考题2-3 关于霍纳规则,有些部分没有完成,故没把解答写上去,我对其 c 问题有疑问,请有解答方法者提供个意见。
给出的代码目前也仅仅为解决问题,没有做优化,请见谅,等有时间了我再好好修改。
插入排序算法伪代码INSERTION-SORT(A)1 for j ←2 to length[A]2 do key ←A[j]3 Insert A[j] into the sorted sequence A[1..j-1]4 i ←j-15 while i > 0 and A[i] > key6 do A[i+1]←A[i]7 i ←i − 18 A[i+1]←keyC#对揑入排序算法的实现:public static void InsertionSort<T>(T[] Input) where T:IComparable<T>{T key;int i;for (int j = 1; j < Input.Length; j++){key = Input[j];i = j - 1;for (; i >= 0 && Input[i].CompareTo(key)>0;i-- )Input[i + 1] = Input[i];Input[i+1]=key;}}揑入算法的设计使用的是增量(incremental)方法:在排好子数组A[1..j-1]后,将元素A[ j]揑入,形成排好序的子数组A[1..j]这里需要注意的是由于大部分编程语言的数组都是从0开始算起,这个不伪代码认为的数组的数是第1个有所丌同,一般要注意有几个关键值要比伪代码的小1.如果按照大部分计算机编程语言的思路,修改为:INSERTION-SORT(A)1 for j ← 1 to length[A]2 do key ←A[j]3 i ←j-14 while i ≥ 0 and A[i] > key5 do A[i+1]←A[i]6 i ←i − 17 A[i+1]←key循环丌变式(Loop Invariant)是证明算法正确性的一个重要工具。
算法导论(第二版)习题答案(英文版)

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