-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathMergeSort.elm
47 lines (33 loc) · 1.09 KB
/
MergeSort.elm
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
module MergeSort exposing (..)
import List exposing (drop, length, take)
import Util exposing (sortingOutputDef)
insertInto : Int -> List Int -> List Int -> List Int
insertInto toInsert lesserList greaterList =
case greaterList of
[] ->
lesserList ++ [ toInsert ]
gHead :: gTail ->
if toInsert > gHead then
insertInto toInsert (lesserList ++ [ gHead ]) gTail
else
lesserList ++ [ toInsert ] ++ greaterList
mergeJoin : List Int -> List Int -> List Int
mergeJoin firstHalf secondHalf =
case firstHalf of
[] ->
secondHalf
fHead :: fTail ->
mergeJoin fTail (insertInto fHead [] secondHalf)
mergeSort : List Int -> List Int
mergeSort inputList =
case inputList of
[] ->
[]
head :: [] ->
[ head ]
_ ->
mergeJoin (mergeSort <| take (length inputList // 2) inputList)
(mergeSort <| drop (length inputList // 2) inputList)
output : List String -> String
output args =
sortingOutputDef args mergeSort