知识卡片
循环不变式连接终止条件与结果
证明循环正确性要依次检查初始化、保持和终止。
插入排序的不变式是每轮开始时前缀 A[1:i−1] 恰由原来这些元素按序组成;循环在 i=n+1 结束,代入不变式即可得整个 A[1:n] 已排序且没有丢失元素。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 第2章 算法入门 · 查看原书依据
At the start of each iteration of the for loop of lines 1–8, the subarray A[1 : i – 1] consists of the elements originally in A[1 : i – 1], but in sorted order.
参考来源
- 位置:《算法导论(第4版)》第 2 章《算法入门》2.1 节“Loop invariants and the correctness of insertion sort”(`OEBPS/chapter002.xhtml`,图 2.2)。
- 结论依据:原文逐一给出初始化、保持、终止三步,并把 `i=n+1` 代入有序前缀不变式证明结果。
- 原始内容:At the start of each iteration of the for loop of lines 1–8, the subarray A [1 : i – 1] consists of the elements originally in A [1 : i – 1], but in sorted order.