aboutsummaryrefslogtreecommitdiff
path: root/comparison/OCaml/msort260000.ml
diff options
context:
space:
mode:
authorShinya Sato <shinnya.sato@gmail.com>2022-05-02 09:49:01 +0900
committerShinya Sato <shinnya.sato@gmail.com>2022-05-02 09:49:01 +0900
commit8c7809d024e85d0a8acfb794a4db41b0e4bc13e6 (patch)
tree25ac81bad7be415d7cf75457275f7161d79f9f10 /comparison/OCaml/msort260000.ml
parent429543c748aaf81b2a4207318eb5a05259469b21 (diff)
downloadinpla-8c7809d024e85d0a8acfb794a4db41b0e4bc13e6.tar.gz
inpla-8c7809d024e85d0a8acfb794a4db41b0e4bc13e6.zip
v0.8.2-1
Diffstat (limited to 'comparison/OCaml/msort260000.ml')
-rw-r--r--comparison/OCaml/msort260000.ml49
1 files changed, 49 insertions, 0 deletions
diff --git a/comparison/OCaml/msort260000.ml b/comparison/OCaml/msort260000.ml
new file mode 100644
index 0000000..a44f41a
--- /dev/null
+++ b/comparison/OCaml/msort260000.ml
@@ -0,0 +1,49 @@
+open Format
+
+let rec split (alist : int list) =
+ match alist with
+ [] -> ([], [])
+ | [a] -> ([a], [])
+ | a::b::cs ->
+ let (m,n) = split cs in
+ (a::m, b::m) ;;
+
+let rec merge (alist : int list) (blist : int list) =
+ match alist with
+ [] -> blist
+ | x::xs ->
+ match blist with
+ [] -> x::xs
+ | y::ys ->
+ if x<y then x::(merge xs (y::ys))
+ else y::(merge (x::xs) ys) ;;
+
+
+let rec msort (alist : int list) =
+ match alist with
+ [] -> []
+ | a::[] -> [a]
+ | a::b::[] -> if a<b then [a;b] else [b;a]
+ | xs ->
+ let (m,n) = split xs
+ in merge (msort m) (msort n) ;;
+
+
+
+let rec mkRandList n =
+ match n with
+ 0 -> []
+ | n -> (Random.int 10000)::(mkRandList (n-1)) ;;
+
+let rec validation alist =
+ let rec validation_cons x blist =
+ match blist with
+ [] -> true
+ | y::ys -> if x<=y then (validation_cons y ys) else false
+ in
+ match alist with
+ [] -> true
+ | x::xs -> validation_cons x xs;;
+
+
+let () = print_bool (validation (msort (mkRandList 260000)));;