sorting??

http://d.hatena.ne.jp/Ehren/20091110
Erlang Lite でソートを書くのが流行っていると聞いて

選択ソート

%% Author: tkuro
%% Created: 2009/11/11
%% Description: selection sort
-module(selection).

%%
%% Include files
%%
-include_lib("eunit/include/eunit.hrl").
%%
%% Exported Functions
%%
-export ([sort/1]).

%% --------------------------------------------------------------------
%% Test Functions
%% --------------------------------------------------------------------

sort_test_() ->
        [
                ?_assertEqual(sort([1,3,2]),[1,2,3]),
                ?_assertEqual(sort([3,2,0,1,5,8,4,2,2,7]),[0,1,2,2,2,3,4,5,7,8])
        ].

%% --------------------------------------------------------------------
%% Func: sort/1
%% Returns: sorted list
%% --------------------------------------------------------------------

sort([]) -> [];
sort( L) -> M = lists:min(L) ,
    [M|sort(L--[M])].

OddEven sort

こっちは並列化が容易(というより並列アレイ向け)。

%% Author: tkuro
%% Created: 2009/11/11
%% Description: odd-even sort
-module(oddeven).

%%
%% Include files
%%
-include_lib("eunit/include/eunit.hrl").
%%
%% Exported Functions
%%
-export ([sort/1]).

%% --------------------------------------------------------------------
%% Test Functions
%% --------------------------------------------------------------------

sort_test_() ->
        [
                ?_assertEqual(sort([1,3,2]),[1,2,3]),
                ?_assertEqual(sort([3,2,0,1,5,8,4,2,2,7]),[0,1,2,2,2,3,4,5,7,8])
        ].

%% --------------------------------------------------------------------
%% Func: sort/1
%% Returns: sorted list
%% --------------------------------------------------------------------

sort(L) -> sort(L, length(L)).

sort(L, 0) -> L;
sort([H|T], N) ->
    if  N band 1 =:= 1 ->  sort([H|oneshot(T)], N-1);
        true           ->  sort(oneshot([H|T]), N-1)
    end.

oneshot([]) -> [];
oneshot([A]) -> [A];
oneshot([F,S|Rest]) ->
    if
        F > S  -> [S,F|oneshot(Rest)];
        true   -> [F,S|oneshot(Rest)]
    end.

すんませんでしたー

というか効率最悪ブラザーズ。

並列化???そのうちやるかも..............