Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,7 @@
|
|||
-module( quicksort ).
|
||||
|
||||
-export( [qsort/1] ).
|
||||
|
||||
qsort([]) -> [];
|
||||
qsort([X|Xs]) ->
|
||||
qsort([ Y || Y <- Xs, Y < X]) ++ [X] ++ qsort([ Y || Y <- Xs, Y >= X]).
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
quick_sort(L) -> qs(L, trunc(math:log2(erlang:system_info(schedulers)))).
|
||||
|
||||
qs([],_) -> [];
|
||||
qs([H|T], N) when N > 0 ->
|
||||
{Parent, Ref} = {self(), make_ref()},
|
||||
spawn(fun()-> Parent ! {l1, Ref, qs([E||E<-T, E<H], N-1)} end),
|
||||
spawn(fun()-> Parent ! {l2, Ref, qs([E||E<-T, H =< E], N-1)} end),
|
||||
{L1, L2} = receive_results(Ref, undefined, undefined),
|
||||
L1 ++ [H] ++ L2;
|
||||
qs([H|T],_) ->
|
||||
qs([E||E<-T, E<H],0) ++ [H] ++ qs([E||E<-T, H =< E],0).
|
||||
|
||||
receive_results(Ref, L1, L2) ->
|
||||
receive
|
||||
{l1, Ref, L1R} when L2 == undefined -> receive_results(Ref, L1R, L2);
|
||||
{l2, Ref, L2R} when L1 == undefined -> receive_results(Ref, L1, L2R);
|
||||
{l1, Ref, L1R} -> {L1R, L2};
|
||||
{l2, Ref, L2R} -> {L1, L2R}
|
||||
after 5000 -> receive_results(Ref, L1, L2)
|
||||
end.
|
||||
Loading…
Add table
Add a link
Reference in a new issue