free pascal 快速排序 堆栈溢出

来源:百度知道 编辑:UC知道 时间:2024/06/20 14:56:48
free pascal 2.1.4版
程序:
procedure qsort(st,ed:integer);
var i,j,z,temp:integer;
begin
i:=st-1;j:=ed+1;
temp:=a[(st+ed)div 2];
while i<j do begin
repeat inc(i) until a[i]>=temp;
repeat dec(j) until a[j]<=temp;
if i<j then begin z:=a[i]; a[i]:=a[j]; a[j]:=z; end;
end;
qsort(st,i); qsort(i+1,ed);
end;

调用排序一个300个的integer数组后;
堆栈溢出……为什么……怎么改?
好的加10分!
急!
我说…………楼下那位……这个和我的程序有什么区别啊……

完整的:
const maxn=301;
var a:array[1..30]of integer; i,j,w,n,ans,t:integer;
procedure qsort(st,ed:integer);
var i,j,z,temp:integer;
begin
i:=st-1;j:=ed+1;
temp:=a[(st+ed)div 2];
while i<j do begin
repeat inc(i) until a[i]>=temp;
repeat dec(j) until a[j]<=temp;
if i<j then begin z:=a[i]; a[i]:=a[j]; a[j]:=z; end;
end;
qsort(st,i); qsort(i+1,ed);
end;
begin
{assig

我觉得可能你这过程中没有退出过程的条件,
qsort(st,i); qsort(i+1,ed);
这两句话在排序号后仍然可以继续调用,
应该为
if st<i then qsort(st,i);
if ed>i+1 then qsort(i+1,ed);
不过我建议你下次写快排时最好用
if ed>i then qsort(i,ed);
因为大部分人都这样做。

直接用pascal自带的程序
C:\FPC\2.2.4\demo\text\qsort.pp
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