

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 CS1110 26 March 2009 Developing array algorithms: initial considerations
5 The invariant as picture: Combining pre- and post-condition Put negative values before non-negative ones via swaps. (Application: separating different types of items; faster than sorting.) Given precondition P: ? (vals in b[0..n-1] are unknown) 0 n P: b < 0 >= 0 0 k n Q: b Swap the values of b[0..n-1] and store in k to truthify Q: (values in b[0..k-1] are < 0, values in b[k..n-1] are >= 0) 6 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 values in b[0.n-1] to truthify postcondition Q: ? 0 n P: b reds whites blues 0 n Q: b (vals in b[0..n-1] are unknown) 7 Remove adjacent duplicates b 1 2 2 4 2 2 7 8 9 9 9 9 change:^0 n into (^) b 1 2 4 2 7 8 9 8 9 9 9 9 0 h n Truthify: b[0..h] = initial values in b[0..n] but with adj dups removed donāt care what is in b[h+1..n] ? 0 n Precondition P: b initial values of b[0..n] unchanged with no duplicates 0 h n Postcondition Q: b