summaryrefslogtreecommitdiffstats
path: root/testasmcomp/quicksort2.cmm
blob: 7bf8e617d8d2c86a8e75e3d183a19947014bc115 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
(***********************************************************************)
(*                                                                     *)
(*                         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 "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)
          (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 [])
          (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))