free pascal 快速排序 堆栈溢出
freepascal2.1.4版程序:procedureqsort(st,ed:integer);vari,j,z,temp:integer;begini:=st-1;j...
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
{assign(input,'group.in'); reset(input);
assign(output,'group.out'); rewrite(output); }
readln(w);
readln(n);
for i:=1 to n do readln(a[i]);
qsort(1,n);
{for i:=1 to n-1 do
for j:=i+1 to n do
if a[i]>a[j] then begin t:=a[i];a[i]:=a[j];a[j]:=t; end; }
ans:=0;
i:=1; j:=n;
repeat
if a[i]+a[j]<=w then begin inc(i); dec(j); end
else dec(j);
inc(ans);
until i>j;
writeln(ans);
{close(input);close(output); }
end. 展开
程序:
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
{assign(input,'group.in'); reset(input);
assign(output,'group.out'); rewrite(output); }
readln(w);
readln(n);
for i:=1 to n do readln(a[i]);
qsort(1,n);
{for i:=1 to n-1 do
for j:=i+1 to n do
if a[i]>a[j] then begin t:=a[i];a[i]:=a[j];a[j]:=t; end; }
ans:=0;
i:=1; j:=n;
repeat
if a[i]+a[j]<=w then begin inc(i); dec(j); end
else dec(j);
inc(ans);
until i>j;
writeln(ans);
{close(input);close(output); }
end. 展开
展开全部
直接用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
sort(i,r);
end;
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
sort(i,r);
end;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
var a:array[1..100] of integer;
i,j,n:integer;
procedure kp(f,l:integer);
var i,j,x:integer;
begin
if l<=f then exit;
i:=f;
j:=l;
x:=a[i];
repeat
while (i<j) and (a[j]<x) do dec(j);
if i<j then begin
a[i]:=a[j];
inc(i);
end;
while (i<j) and (a[i]>x) do inc(i);
if i<j then begin
a[j]:=a[i];
dec(j);
end;
until i=j;
a[i]:=x;
kp(f,i-1);
kp(j+1,l);
end;
begin
readln(n);
for i:=1 to n do read(a[i]);
kp(1,n);
for i:=1 to n do write(a[i],' ');
readln;
end.
把数据量再改下
i,j,n:integer;
procedure kp(f,l:integer);
var i,j,x:integer;
begin
if l<=f then exit;
i:=f;
j:=l;
x:=a[i];
repeat
while (i<j) and (a[j]<x) do dec(j);
if i<j then begin
a[i]:=a[j];
inc(i);
end;
while (i<j) and (a[i]>x) do inc(i);
if i<j then begin
a[j]:=a[i];
dec(j);
end;
until i=j;
a[i]:=x;
kp(f,i-1);
kp(j+1,l);
end;
begin
readln(n);
for i:=1 to n do read(a[i]);
kp(1,n);
for i:=1 to n do write(a[i],' ');
readln;
end.
把数据量再改下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
可能本来就a[i]>=temp 然后你也inc(I); 了,
所以应该把repeat 改成while
还有,每次交换完后要inc(i);dec(j);
所以应该把repeat 改成while
还有,每次交换完后要inc(i);dec(j);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询