aboutsummaryrefslogtreecommitdiff
path: root/comparison/SML/msort-260000.sml
diff options
context:
space:
mode:
authorShinya Sato <shinnya.sato@gmail.com>2022-05-02 09:50:21 +0900
committerShinya Sato <shinnya.sato@gmail.com>2022-05-02 09:50:21 +0900
commitdf4949f8b08cbe3cfd99933d5bb14a373f47f2dd (patch)
treec516c3c45d79a4843114ddd44a83b9ca87250cb7 /comparison/SML/msort-260000.sml
parent7ea48cd96551247ecc763a5e42404ffa3464d0b0 (diff)
parent779860e35f5c67c7fd48ef876784f87afcfdb321 (diff)
downloadinpla-df4949f8b08cbe3cfd99933d5bb14a373f47f2dd.tar.gz
inpla-df4949f8b08cbe3cfd99933d5bb14a373f47f2dd.zip
Merge branch 'develop'
Diffstat (limited to 'comparison/SML/msort-260000.sml')
-rw-r--r--comparison/SML/msort-260000.sml55
1 files changed, 55 insertions, 0 deletions
diff --git a/comparison/SML/msort-260000.sml b/comparison/SML/msort-260000.sml
new file mode 100644
index 0000000..754ef2f
--- /dev/null
+++ b/comparison/SML/msort-260000.sml
@@ -0,0 +1,55 @@
+(* https://stackoverflow.com/questions/25839939/understanding-merge-sort-in-ml *)
+
+fun split [] = ([], [])
+ | split [a] = ([a], [])
+ | split (a::b::cs) =
+ let
+ val (m,n) = split cs
+ in
+ (a::m, b::n)
+ end;
+
+fun merge ([], ys) = ys
+ | merge (xs, []) = xs
+ | merge (x::xs, y::ys) =
+ if x<y then
+ x::merge(xs, y::ys)
+ else
+ y::merge(x::xs, ys);
+
+fun msort [] = []
+ | msort [a] = [a]
+ | msort [a,b] = if a<b then [a,b] else [b,a]
+ | msort xs =
+ let
+ val (m,n) = split xs;
+ in
+ merge (msort m, msort n)
+ end;
+
+
+(* Creates a random list *)
+local
+ val nextInt = Random.randRange(1,10000);
+ val r = Random.rand(1,1);
+in
+ fun mkRandList 0 = []
+ | mkRandList n = (nextInt r)::(mkRandList (n-1))
+end;
+
+
+(* Validation checks *)
+fun validation [] = true
+ | validation (x::xs) =
+ let fun validation_cons x [] = true
+ | validation_cons x (y::ys) =
+ if x<=y then validation_cons y ys
+ else false
+ in
+ validation_cons x xs
+ end;
+
+
+(* Main *)
+validation(msort (mkRandList 260000));
+