Skip to content

Instantly share code, notes, and snippets.

@joslinm
Created November 23, 2012 20:45
Show Gist options
  • Save joslinm/4137210 to your computer and use it in GitHub Desktop.
Save joslinm/4137210 to your computer and use it in GitHub Desktop.
Naive quicksort implementation
less_than(List, Pivot) ->
less_than(List, Pivot, []).
less_than([], Pivot, Acc) ->
Acc;
less_than([H|T], Pivot, Acc) when H < Pivot ->
less_than(T, Pivot, [H|Acc]);
less_than([H|T], Pivot, Acc) when H >= Pivot ->
less_than(T, Pivot, Acc).
greater_than(List, Pivot) ->
greater_than(List, Pivot, []).
greater_than([], Pivot, Acc) ->
Acc;
greater_than([H|T], Pivot, Acc) when H >= Pivot ->
greater_than(T, Pivot, [H|Acc]);
greater_than([H|T], Pivot, Acc) when H < Pivot ->
greater_than(T, Pivot, T).
%% quicksort
quicksort([]) -> [];
quicksort([Pivot|Rest]) ->
lists:flatten([quicksort(less_than(Rest, Pivot)) , Pivot , quicksort(greater_than(Rest, Pivot))]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment