ghost predicate SplitPoint(a: array, n: int) requires 0 <= n <= a.Length reads a { forall i, j :: 0 <= i < n <= j < a.Length ==> a[i] <= a[j] } // Now, quicksort with the same spec as we used for sorting last time. method Quicksort(a: array) modifies a ensures forall i, j :: 0 <= i < j < a.Length ==> a[i] <= a[j] ensures multiset(a[..]) == old(multiset(a[..])) { QuicksortAux(a, 0, a.Length); } // This auxiliary method does most of the work. // Try to fill in a good spec for it. // The body shouldn't need any proof annotations, either. method QuicksortAux(a: array, lo: int, hi: int) { if 2 <= hi - lo { var p := Partition(a, lo, hi); QuicksortAux(a, lo, p); QuicksortAux(a, p + 1, hi); } } // Time permitting, see if you can fill in proof annotations for this method, too. method Partition(a: array, lo: int, hi: int) returns (p: int) { var pivot := a[lo]; var m, n := lo + 1, hi; while m < n { if a[m] < pivot { m := m + 1; } else { a[m], a[n-1] := a[n-1], a[m]; n := n - 1; } } a[lo], a[m - 1] := a[m - 1], a[lo]; return m - 1; }