Question 2.7.2
a. What is the postcondition?
b. What is the loop invariant?
Show ∀j: 1 ≤ j ≤ i, ∑aj = Pk ^ i ≤ n is always true
| 1. Base Step:
before the loop, -- Precondition: ∃a1 |
c. Base step, show invariant
holds: ∀j: 1 ≤ j ≤ i, ∑aj = Pk ^ i ≤ n
|
|||||
2. Inductive Step: Pk
→ Pk+1
|
d. Prove ∀j: 1 ≤ j ≤ i+1, ∑aj
= Pk+1 ^ i+1 ≤ n
|
|||||
| 3. and after the loop completes | e. Prove when terminates: ∀j: 1 ≤ j ≤ i , ∑aj = Pk ^ i ≤ n |
|
|
Insertion sort is relatively simple using insert module.
-- Precondition: ∃a1
procedure insertionSort( a1, a2, a3, ..., an : integers)
i ← 1
∀j: 1 ≤ j < i, aj ≤ ai ^ i ≤ n
while i < n i ← i + 1
a ← insert( ai , a)
-- Postcondition: ∀j: 1 ≤ j < n, aj ≤ aj+1
Question 2.7.3
a. What is the postcondition?
b. What is the loop invariant?
1. Base Step:
|
c. Base step, show invariant holds:
|
||
2. Inductive Step: Pk → Pk+1
-- Postcondition: ∀j: 1 ≤ j < n, aj ≤ aj+1 |
d. Assume what for Pk? Note that k=i. e. Show what for Pk+1? f. Prove ∀j: 1 ≤ j < i+1, aj ≤ aj+1 ^ i+1 ≤ n
|
||
| 3. and after the loop completes | g. Prove when loop terminates:
|