-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
qsort.ml
39 lines (33 loc) · 872 Bytes
/
qsort.ml
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
(* Example: quick sort *)
#use "list.ml"
let rec qsort xs = match xs with
| [] -> []
| [x] -> [x]
| pivot :: rest ->
match list_partition (fun x -> x < pivot) rest with
| (ys, zs) -> list_append (qsort ys) (pivot :: qsort zs)
let l1 = [5; 4; 8; 1; 6; 3; 7; 2]
let l2 = qsort l1
let x0 = list_nth l2 0
let x1 = list_nth l2 1
let x2 = list_nth l2 2
let x3 = list_nth l2 3
let x4 = list_nth l2 4
let x5 = list_nth l2 5
let x6 = list_nth l2 6
let x7 = list_nth l2 7
(*!
// This is C++ code.
#include <cstdio>
int main () { // We use printf in order to output readable assembly code.
std::printf("%d ", x0::val);
std::printf("%d ", x1::val);
std::printf("%d ", x2::val);
std::printf("%d ", x3::val);
std::printf("%d ", x4::val);
std::printf("%d ", x5::val);
std::printf("%d ", x6::val);
std::printf("%d\n", x7::val);
return 0;
}
*)