Advertisement
Guest User

Untitled

a guest
Jun 24th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 0.72 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 1 in
  13.       for i = 1 to n - 1 do
  14.         invariant{forall k: int. 0 <= k < i -> a[k] <= a[0]}
  15.         if a[i] < a[0] then
  16.           let aux = ref a[i] in
  17.             a[i] <- a[!lt];
  18.             a[!lt] <- !aux;
  19.             lt := !lt + 1;
  20.       done;
  21.       let aux = ref a[0] in
  22.         a[0] <- a[!lt];
  23.         a[!lt] <- !aux;
  24.     assert{partitioned a !lt n};
  25.     (a, lt)
  26. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement