pascal快排出错,急求

来源:百度知道 编辑:UC知道 时间:2024/07/02 07:16:27
试了几组数据,最后一两位会排错
完全按标程打的,主程序问题?

program et;
var
a:array[1..100]of integer;
n,i:integer;
procedure qsort(m,n:integer);
var
p,q,temp,mid:integer;
begin
p:=m; q:=n; mid:=a[(m+n)div 2];
repeat
while a[p]<mid do inc(p);
while a[q]>mid do dec(q);
if p<=q then begin temp:=a[p];a[p]:=a[q];a[q]:=temp;end;
inc(p);dec(q);
until p>q;
if p<n then qsort(p,n);
if m<q then qsort(m,q);
end;

begin
read(n);
for i:=1 to n do read(a[i]);
for i:=1 to n do write(a[i],' ');writeln;
qsort(1,n);
for i:=1 to n do write(a[i],' ');
end.
问题已解决~汗
50分先到先得

在pp/demo/test中有sqort.pp
快排程序

repeat
while a[p]<mid do inc(p);
while a[q]>mid do dec(q);
if p<=q then begin temp:=a[p];a[p]:=a[q];a[q]:=temp;end;
inc(p);dec(q);
until p>q;
这段程序应是:
repeat
while a[p]<mid do inc(p);
while a[q]>mid do dec(q);
if p<=q then begin temp:=a[p];a[p]:=a[q];a[q]:=temp;
inc(p);dec(q);
End;
until p>q;
其它都是正确的

我先到我先得吧
你的问题应该出在
procedure qsort(m,n:integer);
这一句吧,n是全局变量,最好不要做过程函数的参数

mid:=a[(m+n)div 2];
最好写成

mid:=a[random(m-n+1)+n];

随机化的比较快