1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
(function "quicksort" (lo: int hi: int a: 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 (> (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 [])
|
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 "quicksort" [lo (- i 1) a] unit)
|
|
|
|
(app "quicksort" [(+ i 1) hi a] unit))
|
|
|
|
[]))
|