13 lines
582 B
Plaintext
13 lines
582 B
Plaintext
%% qsort:qsort(List)
|
|
%% Sort a list of items
|
|
-module(qsort). % This is the file 'qsort.erl'
|
|
-export([qsort/1]). % A function 'qsort' with 1 parameter is exported (no type, no name)
|
|
|
|
qsort([]) -> []; % If the list [] is empty, return an empty list (nothing to sort)
|
|
qsort([Pivot|Rest]) ->
|
|
% Compose recursively a list with 'Front' for all elements that should be before 'Pivot'
|
|
% then 'Pivot' then 'Back' for all elements that should be after 'Pivot'
|
|
qsort([Front || Front <- Rest, Front < Pivot])
|
|
++ [Pivot] ++
|
|
qsort([Back || Back <- Rest, Back >= Pivot]).
|