diff options
| author | Shinya Sato <shinnya.sato@gmail.com> | 2022-05-02 09:50:21 +0900 |
|---|---|---|
| committer | Shinya Sato <shinnya.sato@gmail.com> | 2022-05-02 09:50:21 +0900 |
| commit | df4949f8b08cbe3cfd99933d5bb14a373f47f2dd (patch) | |
| tree | c516c3c45d79a4843114ddd44a83b9ca87250cb7 /comparison | |
| parent | 7ea48cd96551247ecc763a5e42404ffa3464d0b0 (diff) | |
| parent | 779860e35f5c67c7fd48ef876784f87afcfdb321 (diff) | |
| download | inpla-df4949f8b08cbe3cfd99933d5bb14a373f47f2dd.tar.gz inpla-df4949f8b08cbe3cfd99933d5bb14a373f47f2dd.zip | |
Merge branch 'develop'
Diffstat (limited to 'comparison')
27 files changed, 723 insertions, 25 deletions
diff --git a/comparison/Haskell/Makefile b/comparison/Haskell/Makefile index 0042c28..b55259f 100644 --- a/comparison/Haskell/Makefile +++ b/comparison/Haskell/Makefile @@ -1,8 +1,7 @@ CC = stack ghc CFLAGS = -O2 BENCHMARK = bash ../tools/sptest_commands.sh -TARGET = ack3-11 fib38 bsort-40000 isort-40000 qsort-800000 msort-800000 -#TARGET = ack3-11 +TARGET = ack3-11 fib38 bsort-40000 isort-40000 qsort-260000 msort-260000 LOG = log.txt @@ -25,10 +24,10 @@ bsort-40000: bsort-40000.hs isort-40000: isort-40000.hs $(CC) -- $(CFLAGS) $< -qsort-800000: qsort-800000.hs +qsort-260000: qsort-260000.hs $(CC) -- $(CFLAGS) $< -msort-800000: msort-800000.hs +msort-260000: msort-260000.hs $(CC) -- $(CFLAGS) $< diff --git a/comparison/Haskell/bsort-40000.hs b/comparison/Haskell/bsort-40000.hs index 90f208c..c065a32 100644 --- a/comparison/Haskell/bsort-40000.hs +++ b/comparison/Haskell/bsort-40000.hs @@ -15,6 +15,7 @@ bs xs n = xs go xs 0 = xs go xs n = go (bs xs n) (n-1) +bsort [] = [] bsort xs = go xs (length xs -1) {- diff --git a/comparison/Haskell/msort-260000.hs b/comparison/Haskell/msort-260000.hs new file mode 100644 index 0000000..49cae3c --- /dev/null +++ b/comparison/Haskell/msort-260000.hs @@ -0,0 +1,51 @@ +{- +https://stackoverflow.com/questions/25839939/understanding-merge-sort-in-ml +-} +import System.Random + +mysplit [] = ([], []) +mysplit [a] = ([a], []) +mysplit (a:b:cs) = (a:m, b:n) + where + (m,n) = mysplit cs + +merge ([], ys) = ys +merge (xs, []) = xs +merge (x:xs, y:ys) + | x<y = x:merge(xs, y:ys) + | otherwise = y:merge(x:xs, ys) + +msort [] = [] +msort [a] = [a] +msort [a,b] + | a<b = [a,b] + | otherwise = [b,a] +msort xs = merge (msort m, msort n) + where + (m,n) = mysplit xs + +{- +main = print $ msort [3,2,8,5] +-} + + +randomList :: Int -> IO([Int]) +randomList 0 = return [] +randomList n = do + r <- randomRIO (1,10000) + rs <- randomList (n-1) + return (r:rs) + + +validation [] = True +validation (x:xs) = validation_cons x xs +validation_cons x [] = True +validation_cons x (y:ys) = + if x<=y then validation_cons y ys + else False + + +main = do + list <- randomList 260000 + let sorted = msort list + print (validation sorted) diff --git a/comparison/Haskell/qsort-260000.hs b/comparison/Haskell/qsort-260000.hs new file mode 100644 index 0000000..7115121 --- /dev/null +++ b/comparison/Haskell/qsort-260000.hs @@ -0,0 +1,43 @@ +{- +(* http://www.codecodex.com/wiki/Insertion_sort#Standard_ML *) +-} +import System.Random + +qsort [] = [] +qsort [x] = [x] +qsort (x:xs) = (qsort left) ++ [x] ++ (qsort right) + where (left, right) = part x xs + +part pivot [] = ([], []) +part pivot (x:xs) + | x<pivot = (x:below, above) + | otherwise = (below, x:above) + where + (below, above) = part pivot xs + + +{- +main = print $ qsort [3,2,8,5] +-} + + +randomList :: Int -> IO([Int]) +randomList 0 = return [] +randomList n = do + r <- randomRIO (1,10000) + rs <- randomList (n-1) + return (r:rs) + + +validation [] = True +validation (x:xs) = validation_cons x xs +validation_cons x [] = True +validation_cons x (y:ys) = + if x<=y then validation_cons y ys + else False + + +main = do + list <- randomList 260000 + let sorted = qsort list + print (validation sorted) diff --git a/comparison/Inpla/main.sh b/comparison/Inpla/main.sh index f7935fb..6975046 100644 --- a/comparison/Inpla/main.sh +++ b/comparison/Inpla/main.sh @@ -12,8 +12,8 @@ bash ./sptestInpla.sh src/bsort-40000-reuse.in bash ./sptestInpla.sh src/isort-40000.in bash ./sptestInpla.sh src/isort-40000-reuse.in -bash ./sptestInpla.sh src/qsort-800000.in -bash ./sptestInpla.sh src/qsort-800000-reuse.in '-Xms 18 -Xmt 0' +bash ./sptestInpla.sh src/qsort-260000.in +bash ./sptestInpla.sh src/qsort-260000-reuse.in '-Xms 17 -Xmt 0' -bash ./sptestInpla.sh src/msort-800000.in -bash ./sptestInpla.sh src/msort-800000-reuse.in '-Xms 18 -Xmt 0' +bash ./sptestInpla.sh src/msort-260000.in +bash ./sptestInpla.sh src/msort-260000-reuse.in '-Xms 18 -Xmt 0' diff --git a/comparison/Inpla/src/bsort-40000-reuse.in b/comparison/Inpla/src/bsort-40000-reuse.in index 5908f83..d5b23ff 100644 --- a/comparison/Inpla/src/bsort-40000-reuse.in +++ b/comparison/Inpla/src/bsort-40000-reuse.in @@ -18,7 +18,7 @@ make_RandList(ret) >< (int n) // validation checks -valid(ret) >< [] => ret~False; +valid(ret) >< [] => ret~True; valid(ret) >< x:xs => (*L)valid_Cons(ret,x)~xs; valid_Cons(ret, int x) >< [] => ret~True; diff --git a/comparison/Inpla/src/bsort-40000.in b/comparison/Inpla/src/bsort-40000.in index d3c8267..58a63da 100644 --- a/comparison/Inpla/src/bsort-40000.in +++ b/comparison/Inpla/src/bsort-40000.in @@ -18,7 +18,7 @@ make_RandList(ret) >< (int n) // validation checks -valid(ret) >< [] => ret~False; +valid(ret) >< [] => ret~True; valid(ret) >< x:xs => valid_Cons(ret,x)~xs; valid_Cons(ret, int x) >< [] => ret~True; diff --git a/comparison/Inpla/src/isort-40000-reuse.in b/comparison/Inpla/src/isort-40000-reuse.in index 1f7153d..daedf3a 100644 --- a/comparison/Inpla/src/isort-40000-reuse.in +++ b/comparison/Inpla/src/isort-40000-reuse.in @@ -16,7 +16,7 @@ make_RandList(ret) >< (int n) // validation checks -valid(ret) >< [] => ret~False; +valid(ret) >< [] => ret~True; valid(ret) >< x:xs => (*L)valid_Cons(ret,x)~xs; valid_Cons(ret, int x) >< [] => ret~True; diff --git a/comparison/Inpla/src/isort-40000.in b/comparison/Inpla/src/isort-40000.in index 0e3ed32..4947e28 100644 --- a/comparison/Inpla/src/isort-40000.in +++ b/comparison/Inpla/src/isort-40000.in @@ -16,7 +16,7 @@ make_RandList(ret) >< (int n) // validation checks -valid(ret) >< [] => ret~False; +valid(ret) >< [] => ret~True; valid(ret) >< x:xs => valid_Cons(ret,x)~xs; valid_Cons(ret, int x) >< [] => ret~True; diff --git a/comparison/Inpla/src/msort-260000-reuse.in b/comparison/Inpla/src/msort-260000-reuse.in new file mode 100644 index 0000000..7201ac7 --- /dev/null +++ b/comparison/Inpla/src/msort-260000-reuse.in @@ -0,0 +1,51 @@ +// Merge sort +msort(ret) >< [] => ret~[]; +msort(ret) >< x:xs => (*L)msort_tail(ret, x)~xs; + +msort_tail(ret, int n) >< [] => ret~[n]; +msort_tail(ret, int n) >< x:xs => + split(left,right) ~ (*R)(n:x:xs), + msort(a)~left, msort(b)~right, (*L)merge(ret,b)~a; + +merge(ret, snd) >< [] => ret~snd; +merge(ret, snd) >< x:xs => (*L)mergeCC(ret, x, xs)~snd; + +mergeCC(ret, int y, ys) >< [] => ret~(*R)(y:ys); +mergeCC(ret, int y, ys) >< (int x):xs +| x <= y => ret~(x:cnt), (*L)mergeCC(cnt, y, ys) ~ xs +| _ => ret~(y:cnt), (*L)mergeCC(cnt, x, xs) ~ ys; + + +split(right,left) >< [] => right~[], left~[]; +split(right,left) >< x:xs => + right~(*R)(x:cntl), left~cntr, (*L)split(cntr,cntl)~xs; + + +// creates a random list +make_RandList(ret) >< (int n) +| n>0 => ret~(rd:cnt), (*L)make_RandList(cnt)~(n-1) + where rd=rand(10000) +| _ => ret~[]; + + +// validation checks +valid(ret) >< [] => ret~True; +valid(ret) >< x:xs => (*L)valid_Cons(ret,x)~xs; + +valid_Cons(ret, int x) >< [] => ret~True; +valid_Cons(ret, int x) >< (int y):ys +| x<=y => (*L)valid_Cons(ret,y)~ys +| _ => ret~False, Eraser~ys; + +// The `Eraser' is a built-in agent defined for any agents as follows: +//Eraser >< Alpha(a1, ..., a5) => Eraser~a1, ..., Eraser~a5; + + +// Main +const LIST_ELEM=260000; + +make_RandList(rndlist)~(LIST_ELEM), +msort(sorted)~rndlist, +valid(ret)~sorted; + +ret; diff --git a/comparison/Inpla/src/msort-260000.in b/comparison/Inpla/src/msort-260000.in new file mode 100644 index 0000000..efa464d --- /dev/null +++ b/comparison/Inpla/src/msort-260000.in @@ -0,0 +1,51 @@ +// Merge sort +msort(ret) >< [] => ret~[]; +msort(ret) >< x:xs => msort_tail(ret, x)~xs; + +msort_tail(ret, int n) >< [] => ret~[n]; +msort_tail(ret, int n) >< x:xs => + split(left,right) ~ (n:x:xs), + msort(a)~left, msort(b)~right, merge(ret,b)~a; + +merge(ret, snd) >< [] => ret~snd; +merge(ret, snd) >< x:xs => mergeCC(ret, x, xs)~snd; + +mergeCC(ret, int y, ys) >< [] => ret~(y:ys); +mergeCC(ret, int y, ys) >< (int x):xs +| x <= y => ret~(x:cnt), mergeCC(cnt, y, ys) ~ xs +| _ => ret~(y:cnt), mergeCC(cnt, x, xs) ~ ys; + + +split(right,left) >< [] => right~[], left~[]; +split(right,left) >< x:xs => + right~(x:cntl), left~cntr, split(cntr,cntl)~xs; + + +// creates a random list +make_RandList(ret) >< (int n) +| n>0 => ret~(rd:cnt), make_RandList(cnt)~(n-1) + where rd=rand(10000) +| _ => ret~[]; + + +// validation checks +valid(ret) >< [] => ret~True; +valid(ret) >< x:xs => valid_Cons(ret,x)~xs; + +valid_Cons(ret, int x) >< [] => ret~True; +valid_Cons(ret, int x) >< (int y):ys +| x<=y => valid_Cons(ret,y)~ys +| _ => ret~False, Eraser~ys; + +// The `Eraser' is a built-in agent defined for any agents as follows: +//Eraser >< Alpha(a1, ..., a5) => Eraser~a1, ..., Eraser~a5; + + +// Main +const LIST_ELEM=260000; + +make_RandList(rndlist)~(LIST_ELEM), +msort(sorted)~rndlist, +valid(ret)~sorted; + +ret; diff --git a/comparison/Inpla/src/qsort-260000-reuse.in b/comparison/Inpla/src/qsort-260000-reuse.in new file mode 100644 index 0000000..b44e958 --- /dev/null +++ b/comparison/Inpla/src/qsort-260000-reuse.in @@ -0,0 +1,44 @@ +// Quick sort +qsort(ret) >< [] => ret~(*R)[]; +qsort(ret) >< (int x):xs => + (*R)part(x, left, right)~xs, + (*L)qsort(cntl)~left, qsort(cntr)~right, + Append(ret, x:cntl)~cntr; + +// Note: `Append' is implemented as built-in. + + +part(int x, a, b) >< [] => a~(*L)[], b~(*R)[]; +part(int x, a, b) >< (int y):ys +| y<x => b~(*R)(y:cnt), (*L)part(x, a, cnt)~ys +| _ => a~(*R)(y:cnt), (*L)part(x, cnt, b)~ys; + + +// creates a random list +make_RandList(ret) >< (int n) +| n>0 => ret~(rd:cnt), (*L)make_RandList(cnt)~(n-1) + where rd=rand(10000) +| _ => ret~[]; + + +// validation checks +valid(ret) >< [] => ret~True; +valid(ret) >< x:xs => (*L)valid_Cons(ret,x)~xs; + +valid_Cons(ret, int x) >< [] => ret~True; +valid_Cons(ret, int x) >< (int y):ys +| x<=y => (*L)valid_Cons(ret,y)~ys +| _ => ret~False, Eraser~ys; + +// The `Eraser' is a built-in agent defined for any agents as follows: +//Eraser >< Alpha(a1, ..., a5) => Eraser~a1, ..., Eraser~a5; + + +// Main +const LIST_ELEM=260000; + +make_RandList(rndlist)~(LIST_ELEM), +qsort(sorted)~rndlist, +valid(ret)~sorted; + +ret; diff --git a/comparison/Inpla/src/qsort-260000.in b/comparison/Inpla/src/qsort-260000.in new file mode 100644 index 0000000..2f7d63c --- /dev/null +++ b/comparison/Inpla/src/qsort-260000.in @@ -0,0 +1,44 @@ +// Quick sort +qsort(ret) >< [] => ret~[]; +qsort(ret) >< (int x):xs => + part(x, left, right)~xs, + qsort(cntl)~left, qsort(cntr)~right, + Append(ret, x:cntl)~cntr; + +// Note: `Append' is implemented as built-in. + + +part(int x, a, b) >< [] => a~[], b~[]; +part(int x, a, b) >< (int y):ys +| y<x => b~(y:cnt), part(x, a, cnt)~ys +| _ => a~(y:cnt), part(x, cnt, b)~ys; + + +// creates a random list +make_RandList(ret) >< (int n) +| n>0 => ret~(rd:cnt), make_RandList(cnt)~(n-1) + where rd=rand(10000) +| _ => ret~[]; + + +// validation checks +valid(ret) >< [] => ret~True; +valid(ret) >< x:xs => valid_Cons(ret,x)~xs; + +valid_Cons(ret, int x) >< [] => ret~True; +valid_Cons(ret, int x) >< (int y):ys +| x<=y => valid_Cons(ret,y)~ys +| _ => ret~False, Eraser~ys; + +// The `Eraser' is a built-in agent defined for any agents as follows: +//Eraser >< Alpha(a1, ..., a5) => Eraser~a1, ..., Eraser~a5; + + +// Main +const LIST_ELEM=260000; + +make_RandList(rndlist)~(LIST_ELEM), +qsort(sorted)~rndlist, +valid(ret)~sorted; + +ret; diff --git a/comparison/OCaml/Makefile b/comparison/OCaml/Makefile index 94a4ffb..d300b15 100644 --- a/comparison/OCaml/Makefile +++ b/comparison/OCaml/Makefile @@ -1,8 +1,7 @@ CC = ocamlopt CFLAGS = BENCHMARK = bash ../tools/sptest_commands.sh -#TARGET = ack3_11 fib38 bsort40000 isort40000 qsort800000 msort800000 -TARGET = ack3_11 fib38 bsort40000 isort40000 +TARGET = ack3_11 fib38 bsort40000 isort40000 qsort260000 msort260000 LOG = log.txt @@ -25,10 +24,10 @@ bsort40000: bsort40000.ml isort40000: isort40000.ml $(CC) $(CFLAGS) $< -o $@ -qsort800000: qsort-800000.ml +qsort260000: qsort260000.ml $(CC) $(CFLAGS) $< -o $@ -msort800000: msort-800000.ml +msort260000: msort260000.ml $(CC) $(CFLAGS) $< -o $@ diff --git a/comparison/OCaml/bsort40000.ml b/comparison/OCaml/bsort40000.ml index 2a06e85..80c222e 100644 --- a/comparison/OCaml/bsort40000.ml +++ b/comparison/OCaml/bsort40000.ml @@ -1,3 +1,5 @@ +open Format + let rec bs (alist : int list) n = match alist with (x::y::ys) -> @@ -11,9 +13,12 @@ let rec go xs n = 0 -> xs | n -> go (bs xs n) (n-1);; -let bsort xs = go xs (List.length(xs) - 1);; - +let bsort alist = + match alist with + [] -> [] + | xs -> go xs (List.length(xs) - 1);; + let rec mkRandList n = match n with 0 -> [] @@ -31,5 +36,4 @@ let rec validation alist = | x::xs -> validation_cons x xs;; -let main = validation (bsort (mkRandList 40000));; - +let () = print_bool (validation (bsort (mkRandList 40000)));; diff --git a/comparison/OCaml/isort40000.ml b/comparison/OCaml/isort40000.ml index 92e127d..a1d00a5 100644 --- a/comparison/OCaml/isort40000.ml +++ b/comparison/OCaml/isort40000.ml @@ -1,3 +1,5 @@ +open Format + let rec insert x ylist = match ylist with [] -> [x] @@ -30,5 +32,5 @@ let rec validation alist = | x::xs -> validation_cons x xs;; -let main = validation(isort (mkRandList 40000));; +let () = print_bool (validation (isort (mkRandList 40000)));; 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)));; diff --git a/comparison/OCaml/qsort260000.ml b/comparison/OCaml/qsort260000.ml new file mode 100644 index 0000000..983ad50 --- /dev/null +++ b/comparison/OCaml/qsort260000.ml @@ -0,0 +1,38 @@ +open Format + +let rec split pivot (alist : int list) = + match alist with + [] -> ([], []) + | (x::xs) -> + let (below, above) = split pivot xs in + if x<pivot then (x::below, above) + else (below, x::above) ;; + +let rec qsort (alist : int list) = + match alist with + [] -> [] + | pivot::rest -> + let (below, above) = split pivot rest + in (qsort below) @ [pivot] @ (qsort above) ;; + + + + +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 (qsort (mkRandList 260000)));; diff --git a/comparison/Python/Makefile b/comparison/Python/Makefile index fb6de82..21f4b3d 100644 --- a/comparison/Python/Makefile +++ b/comparison/Python/Makefile @@ -1,6 +1,5 @@ COMMAND = python3 -#TARGET = ack3-11.py fib38.py bsort-40000.py isort-40000.py qsort-800000.py msort-800000.py -TARGET = fib38.py bsort-40000.py isort-40000.py qsort-800000.py msort-800000.py +TARGET = dummy.py ack3-11.py fib38.py bsort-40000.py isort-40000.py qsort-260000.py msort-260000.py BENCHMARK = bash ../tools/sptest_scripts.sh LOG = log.txt diff --git a/comparison/Python/dummy.py b/comparison/Python/dummy.py new file mode 100644 index 0000000..98c2206 --- /dev/null +++ b/comparison/Python/dummy.py @@ -0,0 +1,64 @@ +# Python program for implementation of MergeSort +# https://www.geeksforgeeks.org/merge-sort/ +import random + +def mergeSort(arr): + if len(arr) > 1: + + # Finding the mid of the array + mid = len(arr)//2 + + # Dividing the array elements + L = arr[:mid] + + # into 2 halves + R = arr[mid:] + + # Sorting the first half + mergeSort(L) + + # Sorting the second half + mergeSort(R) + + i = j = k = 0 + + # Copy data to temp arrays L[] and R[] + while i < len(L) and j < len(R): + if L[i] < R[j]: + arr[k] = L[i] + i += 1 + else: + arr[k] = R[j] + j += 1 + k += 1 + + # Checking if any element was left + while i < len(L): + arr[k] = L[i] + i += 1 + k += 1 + + while j < len(R): + arr[k] = R[j] + j += 1 + k += 1 + + +def mkRandList ( n ): + a=[] + for i in range(1,n+1): + a.insert(0, random.randint(0,10000)) + return a + + +def validation(alist): + for i in range(len(alist)-1): + if (alist[i] > alist[i+1]): + return False + + return True + + +a = mkRandList(10) +mergeSort(a) +print(validation(a)) diff --git a/comparison/Python/msort-260000.py b/comparison/Python/msort-260000.py new file mode 100644 index 0000000..e7d7011 --- /dev/null +++ b/comparison/Python/msort-260000.py @@ -0,0 +1,64 @@ +# Python program for implementation of MergeSort +# https://www.geeksforgeeks.org/merge-sort/ +import random + +def mergeSort(arr): + if len(arr) > 1: + + # Finding the mid of the array + mid = len(arr)//2 + + # Dividing the array elements + L = arr[:mid] + + # into 2 halves + R = arr[mid:] + + # Sorting the first half + mergeSort(L) + + # Sorting the second half + mergeSort(R) + + i = j = k = 0 + + # Copy data to temp arrays L[] and R[] + while i < len(L) and j < len(R): + if L[i] < R[j]: + arr[k] = L[i] + i += 1 + else: + arr[k] = R[j] + j += 1 + k += 1 + + # Checking if any element was left + while i < len(L): + arr[k] = L[i] + i += 1 + k += 1 + + while j < len(R): + arr[k] = R[j] + j += 1 + k += 1 + + +def mkRandList ( n ): + a=[] + for i in range(1,n+1): + a.insert(0, random.randint(0,10000)) + return a + + +def validation(alist): + for i in range(len(alist)-1): + if (alist[i] > alist[i+1]): + return False + + return True + + +a = mkRandList(260000) +mergeSort(a) +print(validation(a)) diff --git a/comparison/Python/qsort-260000.py b/comparison/Python/qsort-260000.py new file mode 100644 index 0000000..fbca39d --- /dev/null +++ b/comparison/Python/qsort-260000.py @@ -0,0 +1,43 @@ +# Quicksort +import random + +# https://stackoverflow.com/questions/23767787/python-implementation-of-quicksort-fails-to-sort-the-entire-array +def quickSort(arr): + less = [] + pivotList = [] + more = [] + if len(arr) <= 1: + return arr + else: + pivot = arr[0] + for i in arr: + if i < pivot: + less.append(i) + elif i > pivot: + more.append(i) + else: + pivotList.append(i) + + less = quickSort(less) + more = quickSort(more) + return less + pivotList + more + + +def mkRandList ( n ): + a=[] + for i in range(1,n+1): + a.insert(0, random.randint(0,10000)) + return a + + +def validation(alist): + for i in range(len(alist)-1): + if (alist[i] > alist[i+1]): + return False + + return True + + +a = mkRandList(260000) +b = quickSort(a) +print(validation(b)) diff --git a/comparison/SML/Makefile b/comparison/SML/Makefile index 8520e9d..0e4e2e3 100644 --- a/comparison/SML/Makefile +++ b/comparison/SML/Makefile @@ -1,5 +1,5 @@ COMMAND = sml -TARGET = ack3-11.sml fib38.sml bsort-40000.sml isort-40000.sml qsort-800000.sml msort-800000.sml +TARGET = dummy.sml ack3-11.sml fib38.sml bsort-40000.sml isort-40000.sml qsort-260000.sml msort-260000.sml BENCHMARK = bash ../tools/sptest_scripts.sh LOG = log.txt diff --git a/comparison/SML/bsort-40000.sml b/comparison/SML/bsort-40000.sml index 5e6e98b..d9e1a00 100644 --- a/comparison/SML/bsort-40000.sml +++ b/comparison/SML/bsort-40000.sml @@ -12,7 +12,8 @@ fun go xs 0 = xs | go xs n = go (bs xs n) (n-1); -fun bsort xs = go xs (length(xs) - 1); +fun bsort [] = [] + | bsort xs = go xs (length(xs) - 1); (* Creates a random list *) diff --git a/comparison/SML/dummy.sml b/comparison/SML/dummy.sml new file mode 100644 index 0000000..15ff263 --- /dev/null +++ b/comparison/SML/dummy.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 10)); + 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)); + diff --git a/comparison/SML/qsort-260000.sml b/comparison/SML/qsort-260000.sml new file mode 100644 index 0000000..40bf8b7 --- /dev/null +++ b/comparison/SML/qsort-260000.sml @@ -0,0 +1,41 @@ +fun quicksort nil = nil + | quicksort (pivot :: rest) = + let + fun split(nil) = (nil,nil) + | split(x :: xs) = + let + val (below, above) = split(xs) + in + if x < pivot then (x :: below, above) + else (below, x :: above) + end; + val (below, above) = split(rest) + in + quicksort below @ [pivot] @ quicksort above + 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(quicksort(mkRandList 260000)); |
