ocaml/testasmcomp/quicksort.cmm

43 lines
1.6 KiB
Plaintext
Raw Normal View History

(***********************************************************************)
(* *)
(* Caml Special Light *)
(* *)
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1995 Institut National de Recherche en Informatique et *)
(* Automatique. Distributed only by permission. *)
(* *)
(***********************************************************************)
(* $Id$ *)
(function "quicksort" (lo: int hi: int a: addr)
(if (< lo hi)
(let (i lo
j hi
pivot (addraref a hi))
(while (< i j)
(catch
(while 1
(if (>= i hi) exit [])
(if (> (addraref a i) pivot) exit [])
(assign i (+ i 1)))
with [])
(catch
(while 1
(if (<= j lo) exit [])
(if (< (addraref a j) pivot) exit [])
(assign j (- j 1)))
with [])
(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 "quicksort" [lo (- i 1) a] unit)
(app "quicksort" [(+ i 1) hi a] unit))
[]))