我的雲端生活網 - Life+

Monday, September 24, 2007

Quicksort in Erlang

-module(sort).
-export([sort/1]).

sort([]) -> [];
sort([PivotRest]) ->
{Smaller, Bigger} = split(Pivot, Rest),
lists:append(sort(Smaller), [Pivotsort(Bigger)]).

split(Pivot, L) ->
split(Pivot, L, [], []).
split(Pivot, [], Smaller, Bigger) ->
{Smaller, Bigger};
split(Pivot, [HdTl], Smaller, Bigger) when Hd <>
split(Pivot, Tl, [HdSmaller], Bigger);
split(Pivot, [HdTl], Smaller, Bigger) when Hd >= Pivot ->
split(Pivot, T, Smaller, [HdBigger]).

No comments:

Blog Archive