Advertisement
Guest User

Untitled

a guest
Jun 24th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 0.83 KB | None | 0 0
  1. module Partition
  2.  
  3.   use import int.Int
  4.   use import ref.Ref
  5.   use import array.Array
  6.  
  7.   predicate partitioned (a: array int) (pivot: int) (n: int) =
  8.       forall i:int. (0 <= i < pivot ->  a[i] <= a[pivot]) /\ (pivot <= i < n -> a[i] >= a[pivot])
  9.  
  10.   let partition (a: array int) (n: int)
  11.     requires { 2 <= n = length a }
  12.   = let lt = ref 0 in
  13.       let aux = ref a[0] in
  14.         a[0] <- a[n-1];
  15.         a[n-1] <- !aux;
  16.       for i = !lt to n - 2 do
  17.         invariant{0 <= !lt <= i /\ forall k: int. 0 <= k < !lt -> a[k] <= a[n-1]}
  18.         if a[i] < a[n-1] then
  19.           let aux = ref a[i] in
  20.             a[i] <- a[!lt];
  21.             a[!lt] <- !aux;
  22.             lt := !lt + 1;
  23.       done;
  24.       let aux = ref a[n-1] in
  25.         a[n-1] <- a[!lt];
  26.         a[!lt] <- !aux;
  27.     assert{partitioned a !lt n};
  28.     (a, lt)
  29. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement