

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


1
Haikus (5-7-5) seen on Japanese computer monitors Yesterday it worked. Today it is not working. Windows is like that.
Serious error. All shortcuts have disappeared. Screen. Mind. Both are blank.
2
3 Invariant as picture: Combining pre- and post-condition Finding the minimum of an array. Given array b satisfying precondition P, store a value in x to truthify postcondition Q: ? and n >= 0 0 n P: b x is the min of this segment 0 n Q: b (values in 0..n- are unknown) 4 The invariant as picture: Combining pre- and post-condition Put negative values before nonnegative ones. given precondition P: ? 0 n P: b < 0 >= 0 0 k n Q: b (values in 0..n-1 are unknown) Swap the values of b[0..n-1] and store in k to truthify Q: (values in 0..k-1 are < 0, values in k..n-1 are > 0) 5 The invariant as picture: Combining pre- and post-condition Dutch national flag. Swap values of 0..n-1 to put the reds first, then the whites, then the blues. That is, given precondition P, swap value of b[0.n] to truthify postcondition Q: ? 0 n P: b reds whites blues 0 n Q: b (values in 0..n-1 are unknown) 6 Partition algorithm: Given an array b[h..k] with some value x in b[h]: x? h k P: b <= x x >= x h j k Q: b b 3 5 4 1 6 2 3 8 1 h k change: into b1 2 1 3 5 4 6 3 8 h j k x is called the pivot value. x is not a program variable; x just denotes the value initially in b[h]. Swap elements of b[h..k] and store in j to truthify P: b 1 2 3 1 3 4 5 6 8 h j k or
7 Linear search (for value known to be in array) v is in here h k precondition P: b postcondition Q: b Vague spec.: Find first occurrence of v in b[h..k-1], which is known to be there. Better spec.: Store an integer in i to truthify postcondition Q: P: 1. v is not in b[h..i-1]
11 Remove adjacent duplicates b 1 2 2 4 2 2 7 8 9 9 9 9
0 h n Truthify: b[0..h] = initial values in b[0..n] but with adj dups removed
h k Precondition P: b initial values of b[0..k] unchanged with no duplicates h i k Postcondition Q: b