Last active
December 26, 2015 00:52
-
-
Save ka8725/4fbf68c66e274b9d7290 to your computer and use it in GitHub Desktop.
The merge sort algorithm implemented in Erlang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
msort([]) -> | |
[]; | |
msort([H|[]]) -> | |
[H]; | |
msort(List) -> | |
[H|L] = divide2(List), | |
msort(msort(H), msort(L)). | |
msort(List1, List2) -> | |
msort(List1, List2, []). | |
msort([H1|L1], [H2|L2], Acc) when H1 < H2 -> | |
msort(L1, [H2|L2], [H1|Acc]); | |
msort([H1|L1], [H2|L2], Acc) when H2 =< H1 -> | |
msort([H1|L1], L2, [H2|Acc]); | |
msort([H1|L1], [], Acc) -> | |
msort(L1, [], [H1|Acc]); | |
msort([], [H2|L2], Acc) -> | |
msort([], L2, [H2|Acc]); | |
msort([], [], Acc) -> | |
reverse(Acc). |
Author
ka8725
commented
Dec 26, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment