Revision 27bfa7b1b87d081df9d714144cf1e6aae2b87b19 authored by Damien Doligez on 19 August 2004, 12:52:17 UTC, committed by Damien Doligez on 19 August 2004, 12:52:17 UTC

git-svn-id: http://caml.inria.fr/svn/ocaml/version/3.08@6606 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
1 parent 510ea35
Raw File
quicksort2.cmm
(***********************************************************************)
(*                                                                     *)
(*                           Objective Caml                            *)
(*                                                                     *)
(*            Xavier Leroy, projet Cristal, INRIA Rocquencourt         *)
(*                                                                     *)
(*  Copyright 1996 Institut National de Recherche en Informatique et   *)
(*  en Automatique.  All rights reserved.  This file is distributed    *)
(*  under the terms of the Q Public License version 1.0.               *)
(*                                                                     *)
(***********************************************************************)

(* $Id$ *)

(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 (intaref a hi))
        (while (< i j)
          (catch
            (while 1
              (if (>= i hi) exit [])
              (if (> (app cmp (intaref a i) pivot int) 0) exit [])
              (assign i (+ i 1)))
            with [])
          (catch
            (while 1
              (if (<= j lo) exit [])
              (if (< (app cmp (intaref a j) pivot int) 0) exit [])
              (assign j (- j 1)))
           with [])
          (if (< i j)
              (let temp (intaref a i)
                   (intaset a i (intaref a j))
                   (intaset a j temp))
            []))
        (let temp (intaref a i)
             (intaset a i (intaref a hi))
             (intaset 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))
back to top