1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Caml Special Light *)
|
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 1995 Institut National de Recherche en Informatique et *)
|
|
|
|
(* Automatique. Distributed only by permission. *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
(function "cmp" (i: int j: int)
|
|
|
|
(- i j))
|
|
|
|
|
|
|
|
(function "quick" (lo: int hi: int a: addr cmp: addr)
|
|
|
|
(if (< lo hi)
|
|
|
|
(let (i lo
|
|
|
|
j hi
|
|
|
|
pivot (addraref a hi))
|
|
|
|
(while (< i j)
|
1995-06-15 09:09:45 -07:00
|
|
|
(catch
|
|
|
|
(while 1
|
|
|
|
(if (>= i hi) exit [])
|
|
|
|
(if (> (app cmp [(addraref a i) pivot] int) 0) exit [])
|
|
|
|
(assign i (+ i 1)))
|
|
|
|
with [])
|
|
|
|
(catch
|
|
|
|
(while 1
|
|
|
|
(if (<= j lo) exit [])
|
|
|
|
(if (< (app cmp [(addraref a j) pivot] int) 0) exit [])
|
|
|
|
(assign j (- j 1)))
|
|
|
|
with [])
|
1995-06-15 01:17:29 -07:00
|
|
|
(if (< i j)
|
|
|
|
(let temp (addraref a i)
|
|
|
|
(addraset a i (addraref a j))
|
|
|
|
(addraset a j temp))
|
|
|
|
[]))
|
|
|
|
(let temp (addraref a i)
|
|
|
|
(addraset a i (addraref a hi))
|
|
|
|
(addraset a hi temp))
|
|
|
|
(app "quick" [lo (- i 1) a cmp] unit)
|
|
|
|
(app "quick" [(+ i 1) hi a cmp] unit))
|
|
|
|
[]))
|
|
|
|
|
|
|
|
(function "quicksort" (lo: int hi: int a: addr)
|
|
|
|
(app "quick" [lo hi a "cmp"] unit))
|