Programming in Maple: quick sort

version 1.0 for Maple V release 5

G.J. Bex

The quick sort algorithm is very easy to impliment in Maple using the latter's functional programming facilities such as 'select'.

> qsort := proc(l::list)
local lt, ge, rl, i;
if nops(l) = 0 then
[];
else
rl := [seq(l[i], i=2..nops(l))];
lt := select(x -> x < l[1], rl);
ge := select(x -> x >= l[1], rl);
[op(qsort(lt)), l[1], op(qsort(ge))];
fi;
end:

A few test cases:

> qsort([3, 2, 7, 4, 1, 12, 1, 3, 7]);

[Maple Math]

> qsort([]);

[Maple Math]

> qsort([3,2,1]);

[Maple Math]

> qsort([1,2,3]);

[Maple Math]

> qsort([1,1,1]);

[Maple Math]