Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(05): sorting algorithms #8

Merged
merged 11 commits into from
Jun 19, 2024
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ jobs:
run: |
rebar3 dialyzer
# rebar3 eunit -v
rebar3 proper -c true --module prop_boolean,prop_create,prop_manipulating
rebar3 proper -c true --module prop_boolean,prop_create,prop_manipulating,prop_sorting
# rebar3 cover -m 100
29 changes: 25 additions & 4 deletions apps/05-algoritmos-de-ordenacion/src/sorting.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,34 @@
%% @spec quicksort(List :: [integer()]) -> [integer()]
%% @end
%%--------------------------------------------------------------------
quicksort(_List) ->
erlang:throw(not_implemented).
quicksort([]) ->
[];
quicksort([X]) ->
[X];
quicksort([H | T]) ->
List1 = [X || X <- T, X =< H],
List2 = [X || X <- T, X > H],
quicksort(List1) ++ [H] ++ quicksort(List2).

%%--------------------------------------------------------------------
%% @doc Mergesort function.
%% @spec mergesort(List :: [integer()]) -> [integer()]
%% @end
%%--------------------------------------------------------------------
mergesort(_List) ->
erlang:throw(not_implemented).
mergesort([]) ->
[];
mergesort([X]) ->
[X];
mergesort([X1, X2]) ->
quicksort([X1, X2]);
mergesort(List) when is_list(List) ->
{List1, List2} = lists:split(lsize(List) div 2, List),
quicksort(mergesort(List1) ++ mergesort(List2)).

%%%-----------------------------------------------------------------------------
%%% INTERNAL FUNCTIONS
%%%-----------------------------------------------------------------------------
lsize([]) ->
0;
lsize([_ | T]) ->
1 + lsize(T).
Loading