求快速排序的Pascal程序

来源:百度知道 编辑:UC知道 时间:2024/06/05 10:10:07
不要书上的,要简洁明了!!

procedure sort(l,r: longint);
var
i,j,x,y: longint;
begin
i:=l;
j:=r;
x:=a[(l+r) div 2];
repeat
while a[i]<x do
inc(i);
while x<a[j] do
dec(j);
if not(i>j) then
begin
y:=a[i];
a[i]:=a[j];
a[j]:=y;
inc(i);
j:=j-1;
end;
until i>j;
if l<j then
sort(l,j);
if i<r then
sort(i,r);
end;

a是待排序数组,比如是1..n的longint
调用时使用sort(1,n);

来自free pascal的Demo,程序代码和turbo pascal的Demo实际是一样的