Skip to content

Commit b534a4f

Browse files
committed
Update README.md
1 parent fa722e4 commit b534a4f

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

ch02/README.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,7 @@ Loop invariant:
249249
Proof
250250
I:
251251
Prior to the first iteration
252-
-> j = A.length
253-
-> s = { x | x <- A[A.length, A.length] }
252+
-> j = A.length, s = { x | x <- A[A.length, A.length] }
254253
-> A[A.length] is the only element in s
255254
-> I holds.
256255
@@ -276,3 +275,34 @@ T:
276275
277276
I and M and T -> loop invariant holds
278277
```
278+
279+
* Loop invariant and its proof for lines 1-4
280+
```cpp
281+
Loop invariant:
282+
Prior to each iteration of the for loop,
283+
all elements in subarray [ : i - 1] are smaller than anything in subarray [i : ],
284+
and are sorted.
285+
286+
Proof
287+
I:
288+
Prior to the first iteration, [ : i - 1] is empty
289+
-> I holds.
290+
291+
M:
292+
Suppose LI holds before i = i
293+
-> all elements in subarray A[ : i - 1] are smaller than anything in subarray A[i : ], and are sorted.
294+
295+
Applying the property T from part b and above
296+
-> A[i] = max( left_set ), A[i] = min( right_set )
297+
where right_set = { x | x <- A[ i : ]}
298+
left_set = { x | x <- A[ : i ]}
299+
increment i to i + 1
300+
-> LI holds before next iteration
301+
302+
T:
303+
i = A.length is the termination condition
304+
-> subarray A[ : A.length - 1] has been sorted and A[A.length], the only element in subarray A[A.length, A.length], is greater than or equal to the largest element in A[ : A.length - 1]
305+
-> A is sorted.
306+
307+
I.M.T -> LI holds.
308+
```

0 commit comments

Comments
 (0)