Sorting algorithms/Quicksort: Difference between revisions

Content added Content deleted
(C & D)
Line 135: Line 135:
{
{
quick(array, array+length-1);
quick(array, array+length-1);
}


=={{header|D}}==
An implementation much similar to the C one is possible too, this is slower and simpler, derived from the Python one. This is a function template:

import std.stdio;
T[] quickSort(T)(T[] items) {
T[] less, more;
if (items.length <= 1)
return items;
else {
T pivot = items[0];
foreach(el; items[1 .. $])
if (el < pivot)
less ~= el;
else
more ~= el;
return quickSort(less) ~ pivot ~ quickSort(more);
}
}
void main() {
auto a1 = [4, 65, 2, -31, 0, 99, 2, 83, 782, 1];
writefln(quickSort(a1));
auto a2 = [4.0,65.0,2.0,-31.0,0.0,99.0,2.0,83.0,782.0,1.0];
writefln(quickSort(a2));
}
}