算法导论-11.2-4

算法导论-11.2-4
算法导论-11.2-4

算法导论-复习笔记

《算法导论》复习笔记 Chapter 22 基本图算法 有向图邻接链表,计算节点出度和入度的时间复杂度 O(V+E) 开一个degree[]数组,大小为结点个数,复杂度O(V); 遍历邻接链表,经过边uv时,计算出度degree[u]+=1,计算入度degree[v]+=1,复杂度O(E) 将一个多图变成等价无向图,用邻接链表表示,时间复杂度O(V+E) 多图是允许重复边和自循环边的图。 开一个bool数组mark[],大小为节点个数,初始化为false。复杂度O(V)。 对每个顶点u的邻接链表,遍历,令v为u的边所指向的顶点;如果mark[v]=false,将uv加入新图,并将mark[v]设置为true;否则就跳过。复杂度O(E) 再次遍历u的连边,将mark[v]初始化 整体复杂度O(V+E) 伪代码: SOLVE(G,G’) 1 for each vetex u∈G 2 for each v∈ [u] 3 if mark[v]==false 4 mark[v]==true 5 Addedge(G’,u,v) 6 for each v∈[u] 7 mark[v]=false 图G的邻接矩阵表示,给出一个O(V)的算法来判断有向图G中是否存在一个通用汇点。 通用汇点指的是入度|V|-1,但出度为0。 等价问题:给定有向图G的V×V邻接矩阵G,在O(V)时间内判断是否存在一个数k,使得对所有的i有A[i][k]=1,对所有的j有A[k][j]=0,(i≠k,j≠k)

令i和j初值为1,若G[i][j]=0,说明i到j无边,j不可能是通用汇点,令j=j+1;若G[i][j]=1,说明i 到j有边,i不可能是通用汇点,令i=i+1,循环直到i>|V|或者j>|V|;若i>|V|,则不存在通用汇点,若j>|V|,则检查顶点i是否满足要求。 伪代码: 判断是否存在通用汇点 O(V) HAS_UNIVERSL_SINK(G) 1 i=j=1 2 while i≤V and j≤V 3 if G[i][j]==1 4 i=i+1 5 else j=j+1 6 if i>V 7 return false 8 else return CHECK(G,i) CHECK(G,u) 1 for each vertex v∈ 2 if G[u][v]=1 3 return false 4 for each vertex v ∈ 5 if G[v][u]==0& u!=v 6 return false 7 return true 检查点u是否是通用汇点 【宽度优先搜索】 计算无向图BFS后的d值和π值 简单,注意初始节点u的π值写NIL或者写-1

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

Chapter2 Michelle Bodnar,Andrew Lohr April12,2016 Exercise2.1-1 314159264158 314159264158 314159264158 263141594158 263141415958 263141415859 Exercise2.1-2 Algorithm1Non-increasing Insertion-Sort(A) 1:for j=2to A.length do 2:key=A[j] 3://Insert A[j]into the sorted sequence A[1..j?1]. 4:i=j?1 5:while i>0and A[i]

算法导论第二章答案

第二章算法入门 由于时间问题有些问题没有写的很仔细,而且估计这里会存在不少不恰当之处。另,思考题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-1 5 while i > 0 and A[i] > key 6 do A[i+1]←A[i] 7 i ←i ? 1 8 A[i+1]←key C#对揑入排序算法的实现: public static void InsertionSort(T[] Input) where T:IComparable { 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-1

算法导论 第三版 第21章 答案 英

Chapter21 Michelle Bodnar,Andrew Lohr April12,2016 Exercise21.1-1 EdgeP rocessed initial{a}{b}{c}{d}{e}{f}{g}{h}{i}{j}{k} (d,i){a}{b}{c}{d,i}{e}{f}{g}{h}{j}{k} (f,k){a}{b}{c}{d,i}{e}{f,k}{g}{h}{j} (g,i){a}{b}{c}{d,i,g}{e}{f,k}{h}{j} (b,g){a}{b,d,i,g}{c}{e}{f,k}{h}{j} (a,h){a,h}{b,d,i,g}{c}{e}{f,k}{j} (i,j){a,h}{b,d,i,g,j}{c}{e}{f,k} (d,k){a,h}{b,d,i,g,j,f,k}{c}{e} (b,j){a,h}{b,d,i,g,j,f,k}{c}{e} (d,f){a,h}{b,d,i,g,j,f,k}{c}{e} (g,j){a,h}{b,d,i,g,j,f,k}{c}{e} (a,e){a,h,e}{b,d,i,g,j,f,k}{c} So,the connected that we are left with are{a,h,e},{b,d,i,g,j,f,k}, and{c}. Exercise21.1-2 First suppose that two vertices are in the same connected component.Then there exists a path of edges connecting them.If two vertices are connected by a single edge,then they are put into the same set when that edge is processed. At some point during the algorithm every edge of the path will be processed,so all vertices on the path will be in the same set,including the endpoints.Now suppose two vertices u and v wind up in the same set.Since every vertex starts o?in its own set,some sequence of edges in G must have resulted in eventually combining the sets containing u and v.From among these,there must be a path of edges from u to v,implying that u and v are in the same connected component. Exercise21.1-3 Find set is called twice on line4,this is run once per edge in the graph,so, we have that?nd set is run2|E|times.Since we start with|V|sets,at the end 1

算法导论 第三版 第六章 答案 英

Chapter6 Michelle Bodnar,Andrew Lohr December31,2016 Exercise6.1-1 At least2h and at most2h+1?1.Can be seen because a complete binary tree of depth h?1hasΣh?1 i=02i=2h?1elements,and the number of elements in a heap of depth h is between the number for a complete binary tree of depth h?1exclusive and the number in a complete binary tree of depth h inclusive. Exercise6.1-2 Write n=2m?1+k where m is as large as possible.Then the heap consists of a complete binary tree of height m?1,along with k additional leaves along the bottom.The height of the root is the length of the longest simple path to one of these k leaves,which must have length m.It is clear from the way we de?ned m that m= lg n . Exercise6.1-3 If there largest element in the subtee were somewhere other than the root, it has a parent that is in the subtree.So,it is larger than it’s parent,so,the heap property is violated at the parent of the maximum element in the subtree Exercise6.1-4 The smallest element must be a a leaf node.Suppose that node x contains the smallest element and x is not a leaf.Let y denote a child node of x.By the max-heap property,the value of x is greater than or equal to the value of y.Since the elements of the heap are distinct,the inequality is strict.This contradicts the assumption that x contains the smallest element in the heap. Exercise6.1-5 Yes,it is.The index of a child is always greater than the index of the parent, so the heap property is satis?ed at each vertex. 1

算法导论第二章第一节答案

2.1-2 重写过程INSERTION-SORT,使之按非升序(而不是按非降序)排序。 伪代码如下: INSERTION-SORT(A) for j←to length[A] do key←A[J] i←j-1 while i>0 and A[i]和一个值V。 输出:下标i,使得V=A[i],或者当V不再A中出现时为NIL。 写出针对这个问题的线性查找的伪代码,它顺序地扫描整个序列以查找V。利用循环不变式证明算法的正确性。确保所给出的循环不变式满足三个必要的性质。 伪代码如下: LINEAR-SEARCH(A,V) flag←0 for i←1 to length[A] do if A[i]==V then print i flag←1 if flag==0 then print NIL 循环不变式的证明如下:

初始化:首先,先来证明在第一轮迭代之前,它是成立的。此时,flag初始化为0,表示未找到这样一个小标i,使得A[i]=V.若为1,则表示已找到一个或多个这样的下标。那么,很显然,在还未开始之前flag=0是正确的,也就证明了循环不变式在循环的第一轮迭代开始之前是成立的。 保持:接下来证明每一轮循环都能使循环不变式保持成立。我们看到,在第一个for循环体内,随着i逐个从1到遍历完整个数列的过程中,只要有一个下标i,使得A[i]等于V,那么在输出i的同时,将flag重新标记为1,表示已找到。无论接下来是否还有这样满足条件的i出现,flag的值不变,仍为1,反之,若遍历完之后,没有找到这样的一个i,那么flag 在这个for循环中未做任何改变,仍为0。所以,循环不变式的第二个性质也成立。 终止:对此线性查找算法来说,当i大于length[A]的值(即遍历完整个A数列后),for 循环结束。这时,如果flag未改变(即flag=0),则说明未能找到这样的下标i,输出NIL;反之,若已在for循环中被修改为1,则不会运行此步语句,这也就意味着该算法是正确的。 2.1-4 有两个各存放在数组A和B中的n位二进制整数,考虑它们的相加问题。两个整数的和以二进制形式存放在具有(n+1)个元素的数组C中。请给出这个问题的形式化描述,并写出伪代码。 形式化描述如下: 首先,初始化数组C,使其n+1各元素的值都为0。接着,利用for循环用i同时从n到1反向遍历A、B数组,每遍历一步,作一个判断:若A[i]+B[i]>1,则C[i]=C[i]+1;反之,C[i+1]=A[i]+B[i]。最后,当i=0时,for循环结束,此时C数组中存储的即是所求的结果。 伪代码如下: BINRARY-SUM(A,B,C) for i←1 to n+1 do C[i]←0 for i←n to 1 do if A[i]+B[i]>1 then C[i]=C[i]+1 else C[i+1]=A[i]+B[i]

算法导论 第三版 第七章 答案 英

Chapter7 Michelle Bodnar,Andrew Lohr April12,2016 Exercise7.1-1 13199512874212611 13199512874212611 13199512874212611 91913512874212611 95131912874212611 95131912874212611 95819121374212611 95871213194212611 95874131912212611 95874131912212611 95874219122113611 95874261221131911 95874261121131912 Exercise7.1-2 If all elements in the array have the same value,PARTITION returns r.To make PARTITION return q= (p+r)/2 when all elements have the same value,modify line4of the algorithm to say this:if A[j]≤x and j(mod2)= (p+1)(mod2).This causes the algorithm to treat half of the instances of the same value to count as less than,and the other half to count as greater than. Exercise7.1-3 The for loop makes exactly r?p iterations,each of which takes at most constant time.The part outside the for loop takes at most constant time.Since r?p is the size of the subarray,PARTITION takes at most time proportional to the size of the subarray it is called on. Exercise7.1-4 To modify QUICKSORT to run in non-increasing order we need only modify line4of PARTITION,changing≤to≥. 1

算法导论 第三版 第24章 答案 英

Chapter24 Michelle Bodnar,Andrew Lohr April12,2016 Exercise24.1-1 If we change our source to z and use the same ordering of edges to decide what to relax,the d values after successive iterations of relaxation are: s t x y z ∞∞∞∞0 2∞7∞0 25790 25690 24690 Theπvalues are: s t x y z NIL NIL NIL NIL NIL z NIL z NIL NIL z x z s NIL z x y s NIL z x y s NIL Now,if we change the weight of edge(z,x)to4and rerun with s as the source,we have that the d values after successive iterations of relaxation are: s t x y z 0∞∞∞∞ 06∞7∞ 06472 02472 0247?2 Theπvalues are: s t x y z NIL NIL NIL NIL NIL NIL s NIL s NIL NIL s y s t NIL x y s t NIL x y s t 1

Note that these values are exactly the same as in the worked example.The di?erence that changing this edge will cause is that there is now a negative weight cycle,which will be detected when it considers the edge(z,x)in the for loop on line5.Since x.d=4>?2+4=z.d+w(z,x),it will return false on line7. Exercise24.1-2 Suppose there is a path from s to v.Then there must be a shortest such path of lengthδ(s,v).It must have?nite length since it contains at most|V|?1 edges and each edge has?nite length.By Lemma24.2,v.d=δ(s,v)<∞upon termination.On the other hand,suppose v.d<∞when BELLMAN-FORD ter-minates.Recall that v.d is monotonically decreasing throughout the algorithm, and RELAX will update v.d only if u.d+w(u,v)

《算法导论2版》课后答案_加强版2

1 算法导论第三次习题课 2008.12.17

2 19.1-1 如果x 是根节点:degree[sibling[x]] > sibling[x] 如果x 不是根节点:degree[sibling[x]] = sibling[x] –119.1-3 略

3 19.2-2 过程略( union 操作) 19.2-3 (1)decrease-key (2)extract-min 过程略 19.2-6 算法思想:找到堆中最小值min ,用min-1代替-∞. [遍历堆中树的根节点]

4 15.1-1 15.1-2 略P195 或P329 15.1-4 f i [j]值只依赖f i [j-1]的值,从而可以从2n 压缩为2个。再加上f*、l*、l i [j]。 Print-station(l, I, j ) //i 为线路,j 为station if j>1 then Print-station(l, l i [j], j-1 ) print “line”I, “station”j;

5 15.2-1 略(见课本) 15.2-2 15.2-4 略 MATRIX-CHAIN-MULTIPLY(A, s, i, j) if j>i x= MATRIX-CHAIN-MULTIPLY(A, s, s(i,j), j) y= MATRIX-CHAIN-MULTIPLY(A, s, s(i,j)+1,j) return MATRIX-MULTIPLY(x, y) else return A i

6 15.3-1 (归纳法)递归调用 枚举15.3-2 没有效果,没有重叠子问题 15.3-4 略 (3)n Ο3/2(4/) n n Θ

“算法分析与优化”教学大纲

“算法分析与优化”教学大纲 [2009年版] 课程定位 《算法分析与优化》是软件开发人员必修专业课,软件的效率和稳定性取决于软件中所采用的算法;对于一般程序员和软件类专业学生,学习算法设计与分析课程,可以开阔编程思路,编写出优质程序。 课程总体目标 通过对常用的、有代表性的算法的研究,让学生理解并掌握算法设计的基本技术。培养学生分析算法复杂度的初步能力,锻炼其逻辑思维能力和想象力,并使之了解算法理论的发展。鼓励学生运用算法知识解决各自学科的实际问题,培养学生的独立科研的能力和理论联系实践的能力。 课程内容简介 课程共分9章,首先在第1章介绍了算法的基本概念,接着对算法的计算复杂性和算法的描述做了简要介绍。 第2章介绍了递归与分治策略,这是设计有效算法的常用策略。 第3章介绍了动态规划算法,以具体实例详述了动态规划算法的设计思想,适用性已经算法的设计要点。 第4章介绍了贪心算法,这也是一种重要的算法设计策略,它与动态规划算法有一定的联系,但效率更高。按贪心算法设计出的许多算法能产生最优解。其中有许多经典问题和典型算法可供学习和使用。 第5章和第6章分别介绍了回溯法和分支限界法。这两章内容适合于求解难解的问题,其解题思想各具特色。 第7章介绍了概率算法,为许多难解问题提供了高效的解决途径。 第8章介绍了NP完全性理论,首先介绍了计算模型,确定性和非确定性图灵机,然后进一步深入介绍NP完全性理论。 第9章则是通过实例介绍算法设计中常用的算法优化策略。。 课程总学时 48学时 教材 《算法设计与分析》(第2版),王晓东编著,清华大学出版社,2008年 参考书 《算法导论》(第2版),Thomas H.Cormen,Charles E.Leiserson 等著,潘金贵顾铁成等译,机械工业出版社

算法导论第四章

算法导论第四章 摘自:百度文库 https://www.360docs.net/doc/7012035802.html,/link?url=QunHIQ6sgLN_uMads3Llc8mtkAKSkrn4OI9SuM_g3Tj_0a5N 2fkRVV03F9-iiPOgdcm7zAj54KToWqfeFvNffHr-WaJpeqXzWkDRzewxuhm https://www.360docs.net/doc/7012035802.html,/tanky-woo/archive/2011/04/12/144020.html 这一章讲的是递归式(recurrence),递归式是一组等式或不等式,它所描述的函数是用在更小的输入下该函数的值来定义的。 本章讲了三种方法来解递归式,分别是代换法,递归树方法,主方法。 1.代换法(Substitution method)(P38~P40) 定义:即在归纳假设时,用所猜测的值去代替函数的解。 用途:确定一个递归式的上界或下界。 缺点:只能用于解的形式很容易猜的情形。 总结:这种方法需要经验的积累,可以通过转换为先前见过的类似递归式来求解。 2.递归树方法(Recursion-tree method) 起因:代换法有时很难得到一个正确的好的猜测值。 用途:画出一个递归树是一种得到好猜测的直接方法。 分析(重点):在递归树中,每一个结点都代表递归函数调用集合中一个子问题的代价。将递归树中每一层内的代价相加得到一个每层代价的集合,再将每层的代价相加得到递归式所有层次的总代价。 总结:递归树最适合用来产生好的猜测,然后用代换法加以验证。 递归树扩展过程:①.第二章2.3.2节分析分治法时图2-5(P21~P22)的构造递归树过程;②.第四章P41图4-1的递归树构造过程;这两个图需要好好分析。 3.主方法(Master method) 优点:针对形如T(n) = aT(n/b) + f(n)的递归式 缺点:并不能解所有形如上式的递归式的解。 具体分析:

算法导论习题答案

Chapter2 Getting Start 2.1 Insertion sort 2.1.2 将Insertion-Sort 重写为按非递减顺序排序 2.1.3 计算两个n 位的二进制数组之和 2.2 Analyzing algorithms 当前n-1个元素排好序后,第n 个元素已经是最大的元素了. 最好时间和最坏时间均为2()n Θ 2.3 Designing algorithms 2.3.3 计算递归方程的解 22()2(/2)2,1k if n T n T n n if n for k =?=?+ = >? (1) 当1k =时,2n =,显然有()lg T n n n = (2) 假设当k i =时公式成立,即()lg 2lg 22i i i T n n n i ===?, 则当1k i =+,即12i n +=时, 2.3.4 给出insertion sort 的递归版本的递归式 2.3-6 使用二分查找来替代insertion-sort 中while 循环内的线性扫描,是否可以将算法的时间提高到(lg )n n Θ? 虽然用二分查找法可以将查找正确位置的时间复杂度降下来,但

是移位操作的复杂度并没有减少,所以最坏情况下该算法的时间复杂度依然是2()n Θ 2.3-7 给出一个算法,使得其能在(lg )n n Θ的时间内找出在一个n 元素的整数数组内,是否存在两个元素之和为x 首先利用快速排序将数组排序,时间(lg )n n Θ,然后再进行查找: Search(A,n,x) QuickSort(A,n); i←1; j←n; while A[i]+A[j]≠x and i,()()b b n a n +=Θ 0a >时,()()2b b b b n a n n n +<+= 对于121,2b c c ==,12()b b b c n n a c n <+< 0a <时,()b b n a n +<

算法导论 第三版 第十九章 答案 英

Chapter19 Michelle Bodnar,Andrew Lohr April12,2016 Exercise19.2-1 First,we take the subtrees rooted at24,17,and23and add them to the root list.Then,we set H.min to18.Then,we run consolidate.First this has its degree2set to the subtree rooted at18.Then the degree1is the subtree rooted at38.Then,we get a repeated subtree of degree2when we consider the one rooted at24.So,we make it a subheap by placing the24node under18. Then,we consider the heap rooted at17.This is a repeat for heaps of degree1, so we place the heap rooted https://www.360docs.net/doc/7012035802.html,stly we consider the heap rooted at23,and then we have that all the di?erent heaps have distinct degrees and are done,setting H.min to the smallest,that is,the one rooted at17. The three heaps that we end up with in our root list are: 23 17 38 30 41 and 1

《算法导论》CLRS算法C++实现

《算法导论》CLRS算法C++实现(一)P11 插入排序 过几个月要面试了,最近在看《算法导论》,想把里面的算法都用C++实现一遍。今天是第一个算法,比较简单。 第二章算法入门 插入排序 伪代码实现 INSERTION-SORT(A) 《算法导论》P10 1for j ← 2 to length[A] 2do key ← A[j] 3//Insert A[j] into the sorted sequence A[1 ‥ j - 1]. 4i ← j - 1 5while i > 0 and A[i] > key 6do A[i + 1] ← A[i] 7i ← i - 1 8 A[i + 1] ← key C++代码实现 1 #include 2 3using namespace std; 4 5void insertSort(int *arr, int n) 6 { 7int i, j, key; 8for(j = 1; j < n; j++) 9 { 10 key = arr[j]; 11 i = j - 1; 12while((i >= 0) && (arr[i] > key)) 13 { 14 arr[i + 1] = arr[i]; 15 i--; 16 } 17 arr[i + 1] = key; 18 } 19 } 20https://www.360docs.net/doc/7012035802.html, 21int main() 22 { 23int a[] = {2, 4, 32, 64, 67, 34, 78, 23, 3456, 2345, 123, 1, 3};

Ch10算法导论 第三版 第十章 答案 英

Chapter10 Michelle Bodnar,Andrew Lohr April12,2016 Exercise10.1-1 4 41 413 41 418 41 Exercise10.1-2 We will call the stacks T and R.Initially,set T.top=0and R.top=n+1. Essentially,stack T uses the?rst part of the array and stack R uses the last part of the array.In stack T,the top is the rightmost element of T.In stack R, the top is the leftmost element of R. Algorithm1PUSH(S,x) 1:if S==T then 2:if T.top+1==R.top then 3:error“over?ow” 4:else 5:T.top=T.top+1 6:T[T.top]=x 7:end if 8:end if 9:if S==R then 10:if R.top?1==T.top then 11:error“over?ow” 12:else 13:R.top=R.top?1 14:T[T.top]=x 15:end if 16:end if 1

Algorithm2POP(S) if S==T then if T.top==0then error“under?ow” else T.top=T.top?1. return T[T.top+1] end if end if if S==R then if R.top==n+1then error“under?ow” else R.top=R.top+1. return R[R.top?1] end if end if Exercise10.1-3 4 41 413 13 138 38 Exercise10.1-4 Algorithm3ENQUEUE if Q.head==Q.tail+1,or Q.head==1and Q.tail==Q.length then error“over?ow” end if Q[Q.tail]=x if Q.tail==Q.length then Q.tail=1 else Q.tail=Q.head+1 end if Exercise10.1-5 As in the example code given in the section,we will neglect to check for over?ow and under?ow errors. 2

MIT-算法导论-第二章-读书笔记

MIT-算法导论读书笔记-第二章给出的代码目前也仅仅为解决问题,没有做优化,请见谅,等有时间了我再好好修改。插入排序算法伪代码 INSERTION-SORT(A) 1for j←2 to length[A] 2do key←A[j] 3//Insert A[j] into the sorted sequence A[1..j-1] 4i←j-1 5while i>0 and A[i]>key 6do A[i+1] ←A[i] 7i←i-1 8A[i+1] ←key C#对插入排序算法的实现: public static void InsertionSort(T[] Input) where T:IComparable { 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; } } Insertion-sort的C语言实现: /* insertion-sort(A) 插入排序算法 */ #include "stdio.h" void main() { int A[10],i,j,key; printf("pls input 10 numbers!\n>>"); for(i=0;i<=9;++i) scanf("%d",&A[i]); for(j=1;j<=9;++j)

{ key=A[j]; i=j-1; while(i>0 && A[i]>key) { A[i+1]=A[i]; i=i-1; } A[i+1]=key; } printf("the sort is:\n"); for(i=0;i<=9;++i) printf("%d\n",A[i]); } 插入算法的设计使用的是增量(incremental)方法:在排好子数组A[1..j-1]后,将元素A[j]插入,形成排好序的子数组A[1..j] 这里需要注意的是由于大部分编程语言的数组都是从0开始算起,这个与伪代码认为的数组的数是第1个有所不同,一般要注意有几个关键值要比伪代码的小1. 如果按照大部分计算机编程语言的思路,修改为: INSERTION-SORT(A) 1for j←1 to length[A] 2do key←A[j] 3i←j-1 4while i>=0 and A[i]>key 5do A[i+1] ←A[i] 6i←i-1 7A[i+1] ←key 循环不变式(Loop Invariant)是证明算法正确性的一个重要工具。对于循环不变式,必须证明它的三个性质: 初始化(Initialization):它在循环的第一轮迭代开始之前,应该是正确的。 保持(Maintenance):如果在循环的某一次迭代开始之前它是正确的,那么,在下一次迭代开始之前,它也是正确的。 终止(Termination):当循环结束时,不变式给了我们一个有用的性质,它有助于表明算法是正确的。 运用循环不变式对插入排序算法的正确性进行证明: 初始化:j=2,子数组A[1..j-1]只包含一个元素A[1],显然它是已排序的。 保持:若A[1..j-1]是已排序的,则按照大小确定了插入元素A[j]位置之后的数组A[1..j]显然也是已排序的。 终止:当j=n+1时,退出循环,此时已排序的数组是由A[1],A[2],A[3]…A[n]组成的A[1..n],此即原始数组A。 练习 2.1-1:以图2-2为模型,说明INSERTION-SORT在数组A=<31,41,59,26,41,58>上的执行过程。 (2.1-1

相关文档
最新文档