pascalabcnet/TestSuite/CompilationSamples/QuickSort.pas
Бондарев Иван e6e67c193c initial commit
2015-05-14 21:35:07 +02:00

56 lines
924 B
ObjectPascal
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Áûñòðàÿ ñîðòèðîâêà ×. Õîàðà
uses ArrayLib;
/// Áûñòðàÿ ñîðòèðîâêà
procedure QuickSort(a: array of integer);
/// Ðàçäåëåíèå a[l]..a[r] íà ÷àñòè a[l]..a[q] <= a[q+1]..a[r]
function Partition(l,r: integer): integer;
begin
var i := l - 1;
var j := r + 1;
var x := a[l];
while True do
begin
repeat
Inc(i);
until a[i]>=x;
repeat
Dec(j);
until a[j]<=x;
if i<j then
Swap(a[i],a[j])
else
begin
Result := j;
exit;
end;
end;
end;
/// Ñîðòèðîâêà ÷àñòåé
procedure sort(l,r: integer);
begin
if l>=r then exit;
var j := Partition(l,r);
sort(l,j);
sort(j+1,r);
end;
begin
sort(0,a.Length-1)
end;
const n = 20;
var a: array of integer;
begin
CreateRandom(a,n);
writeln('Äî ñîðòèðîâêè: ');
WriteArray(a);
QuickSort(a);
writeln('Ïîñëå ñîðòèðîâêè: ');
WriteArray(a);
end.