procedure quicksort(var a:array of integer; Lo,Hi: integer);
procedure sort(l,r: integer); var i,j,x:integer; begin i:=l; j:=r; x:=a[random(r-l+1)+l]; repeat while a[i]>x do i:=i+1; while x>a[j] do j:=j-1; if i<=j then begin if a[i]<a[j] then swap(a[i],a[j]); i:=i+1; j:=j-1; end; until i>=j; if l<j then sort(l,j); if i<r then sort(i,r); end;
begin; randomize; sort(Lo,Hi) end;
begin readln(n); setlength(ar,n); n:=n-1; writeln('Array:'); for i:=0 to n do begin ar[i]:=random(10); write(ar[i]:4); end; quicksort(ar,0,n); writeln; writeln('Final array:'); for i:=0 to n do write(ar[i]:4); end.
Пример ввода: 5 Пример вывода: Array: 8 7 5 9 4 Final array: 9 8 7 5 4
begin
var a:=ArrRandom(ReadInteger('n='),-99,99); a.Println;
a.SortedDescending.Println;
end.
Тестовое решение
n=15
-64 -10 56 -78 35 -53 61 -12 17 29 -45 -13 73 24 1
73 61 56 35 29 24 17 1 -10 -12 -13 -45 -53 -64 -78
Var
ar:array of integer;
i,n:integer;
procedure quicksort(var a:array of integer; Lo,Hi: integer);
procedure sort(l,r: integer);
var
i,j,x:integer;
begin
i:=l;
j:=r;
x:=a[random(r-l+1)+l];
repeat
while a[i]>x do i:=i+1;
while x>a[j] do j:=j-1;
if i<=j then
begin
if a[i]<a[j] then swap(a[i],a[j]);
i:=i+1;
j:=j-1;
end;
until i>=j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end;
begin;
randomize;
sort(Lo,Hi)
end;
begin
readln(n);
setlength(ar,n);
n:=n-1;
writeln('Array:');
for i:=0 to n do
begin
ar[i]:=random(10);
write(ar[i]:4);
end;
quicksort(ar,0,n);
writeln;
writeln('Final array:');
for i:=0 to n do
write(ar[i]:4);
end.
Пример ввода:
5
Пример вывода:
Array:
8 7 5 9 4
Final array:
9 8 7 5 4