看到《算法导论》第二章中的用于证明插入排序的loop invariant,不太理解,搜了一下找到一个解释:
算法导论第二章中的原文是:We state these properties ofA[1 ‥ j -1] formally as a loop invariant。其中举的例子是插入排序,每次循环从数组A中取出第j个元素插入有序区A[1 .. j-1],然后递增j。这样A[1 .. j-1]的有序性始终得到保持,这就是所谓的“循环不变”了。 这个概念主要用来检验算法的正确性。 原文如下:We use loop invariants to help us understand why an algorithm is correct. We must show three things about a loop invariant:
1. 初始化(循环第一次迭代之前)的时候,A[1 ‥ j -1]的“有序性”是成立的; 单就这个插入排序来说的话,这个解释确实不错,其实就是对原文的翻译分析。但是对这个loop invariant的作用还是觉得模模糊糊的,书中也说是类似归纳法的一个证明方法,只是最后一步不同而已。 一直就很头痛有关证明的东西,一些数学公式什么的,很头大,这是另一个关于loop invariant的introduction: http://www.cs.uofs.edu/~mccloske/courses/cmps144/invariants_lec.html
|