Created
November 23, 2012 20:45
-
-
Save joslinm/4137210 to your computer and use it in GitHub Desktop.
Naive quicksort implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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