pascalabcnet/InstallerSamples/LINQ/QuickSortLinq.pas

19 lines
387 B
ObjectPascal
Raw Permalink Normal View History

2015-07-22 22:34:46 +03:00
function QuickSort(a: sequence of integer): sequence of integer;
2015-05-14 22:35:07 +03:00
begin
if a.Count = 0 then
Result := a
2015-07-22 22:34:46 +03:00
else
2015-05-14 22:35:07 +03:00
begin
2015-07-22 22:34:46 +03:00
var head := a.First();
var tail := a.Skip(1);
Result := QuickSort(tail.Where(x->x<=head)) +
head +
QuickSort(tail.Where(x->x>head));
2015-05-14 22:35:07 +03:00
end;
2015-07-22 22:34:46 +03:00
end;
2015-05-14 22:35:07 +03:00
begin
2015-07-22 22:34:46 +03:00
var a := ArrRandom(20);
2015-05-14 22:35:07 +03:00
a.Println;
2015-07-22 22:34:46 +03:00
QuickSort(a).Println;
2015-05-14 22:35:07 +03:00
end.