6 Elliott Brothers Ltd., Hertfordshire, England, U.K.
8 Communications of the ACM archive
9 Volume 4 , Issue 7 (July 1961) table of contents
20 use array.IntArraySorted
24 val partition (a: array int) (m n: int) (i j: ref int) (ghost x: ref int) :
26 requires { 0 <= m < n < length a }
28 ensures { m <= !j < !i <= n }
29 ensures { permut_sub (old a) a m (n+1) }
30 ensures { forall r:int. m <= r <= !j -> a[r] <= !x }
31 ensures { forall r:int. !j < r < !i -> a[r] = !x }
32 ensures { forall r:int. !i <= r <= n -> a[r] >= !x }
36 predicate qs_partition (t1 t2: array int) (m n i j: int) (x: int) =
37 permut_sub t1 t2 m (n+1) /\
38 (forall k:int. m <= k <= j -> t2[k] <= x) /\
39 (forall k:int. j < k < i -> t2[k] = x) /\
40 (forall k:int. i <= k <= n -> t2[k] >= x)
42 let rec quicksort (a:array int) (m n:int) : unit
43 requires { 0 <= m <= n < length a }
45 ensures { permut_sub (old a) a m (n+1) }
46 ensures { sorted_sub a m (n+1) }
50 let ghost x = ref 42 in
51 partition a m n i j x;
54 assert { qs_partition (a at L1) a m n !i !j !x };
57 assert { qs_partition (a at L2) a m n !i !j !x }
60 let qs (a:array int) : unit
61 ensures { permut_all (old a) a }
63 = if length a > 0 then quicksort a 0 (length a - 1)