Last active
August 21, 2016 10:36
-
-
Save carlosgaldino/9c77d1e8924a86018a60d44e1518d726 to your computer and use it in GitHub Desktop.
This file contains 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
-module(msort). | |
-export([sort/1]). | |
sort([]) -> | |
[]; | |
sort([X]) -> | |
[X]; | |
sort(List) -> | |
{Xs, Ys} = split(List), | |
merge(sort(Xs), sort(Ys)). | |
merge(Xs, []) -> | |
Xs; | |
merge([], Ys) -> | |
Ys; | |
merge([X|Xs], [Y|Ys]) -> | |
case X =< Y of | |
true -> | |
[X|merge(Xs, [Y|Ys])]; | |
false -> | |
[Y|merge([X|Xs], Ys)] | |
end. | |
split(List) -> | |
lists:split(length(List) div 2, List). |
This file contains 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
-module(msort_tests). | |
-include_lib("eunit/include/eunit.hrl"). | |
msort_test() -> | |
?assertEqual([1, 2, 3, 4], msort:sort([1, 2, 3, 4])), | |
?assertEqual([1, 2, 3, 4], msort:sort([1, 3, 2, 4])), | |
?assertEqual([1, 2, 3, 4], msort:sort([4, 3, 2, 1])), | |
?assertEqual([1, 2, 3], msort:sort([1, 2, 3])), | |
?assertEqual([1, 2, 3], msort:sort([2, 1, 3])), | |
?assertEqual([1, 2, 3], msort:sort([3, 2, 1])). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment