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 "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
|
1996-10-24 09:16:11 -07:00
|
|
|
pivot (intaref a hi))
|
1995-06-15 01:17:29 -07:00
|
|
|
(while (< i j)
|
1995-06-15 09:09:45 -07:00
|
|
|
(catch
|
|
|
|
(while 1
|
|
|
|
(if (>= i hi) exit [])
|
1996-10-24 09:16:11 -07:00
|
|
|
(if (> (app cmp [(intaref a i) pivot] int) 0) exit [])
|
1995-06-15 09:09:45 -07:00
|
|
|
(assign i (+ i 1)))
|
|
|
|
with [])
|
|
|
|
(catch
|
|
|
|
(while 1
|
|
|
|
(if (<= j lo) exit [])
|
1996-10-24 09:16:11 -07:00
|
|
|
(if (< (app cmp [(intaref a j) pivot] int) 0) exit [])
|
1995-06-15 09:09:45 -07:00
|
|
|
(assign j (- j 1)))
|
|
|
|
with [])
|
1995-06-15 01:17:29 -07:00
|
|
|
(if (< i j)
|
1996-10-24 09:16:11 -07:00
|
|
|
(let temp (intaref a i)
|
|
|
|
(intaset a i (intaref a j))
|
|
|
|
(intaset a j temp))
|
1995-06-15 01:17:29 -07:00
|
|
|
[]))
|
1996-10-24 09:16:11 -07:00
|
|
|
(let temp (intaref a i)
|
|
|
|
(intaset a i (intaref a hi))
|
|
|
|
(intaset a hi temp))
|
1995-06-15 01:17:29 -07:00
|
|
|
(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))
|