X
xzb_97
Unregistered / Unconfirmed
GUEST, unregistred user!
三个数的排序,一F9就死机,为什么?帮看一下问题在哪?还有就是如果有更好的写法可以再加分!
procedure merge(x:array of Integer;
s:array of Integer;
l:Integer;
m:Integer;
n:Integer);
var
i,j,k:Integer;
begin
i:=l;
j:=m+1;
k:=l;
while (i<=m) and (j<=n)do
begin
if (x< x[j]) then
begin
s[k]:=x;
i:=i+1;
k:=k+1;
end
else
begin
s[k]:=x[j];
j:=j+1;
k:=k+1;
end;
end;
while (i<=m)do
begin
s[k+1]:=x[i+1];
end;
while (j<=n)do
begin
s[k+1]:=x[j+1];
end;
end;
//
procedure mergpass(x:array of Integer;
s:array of Integer;
n:Integer;
l:Integer);
var
i,j:Integer;
begin
i:=1;
while(i+2*l-1=n)do
begin
merge(x,s,i,i+l-1,i+2*l-1);
i:=i+2*l;
end;
if (i+l-1<=n) then
begin
merge(x,s,i,i+l-1,n);
end
else
begin
for j:=i to ndo
begin
s[j]:=x[j];
end;
end;
end;
//
procedure mergesort(r:array of Integer;
n:Integer);
var
i,len:Integer;
s:array[1..3] of Integer;
begin
len:=1;
while(len<n)do
begin
mergpass(r,s,n,len);
for i:=1 to ndo
begin
r:=s;
len:=2*len;
end;
end;
end;
//
procedure TForm1.Button1Click(Sender: TObject);
var
i:Integer;
a:array[1..3] of Integer;
begin
a[1]:=StrToInt(Edit1.Text);
a[2]:=StrToInt(Edit2.Text);
a[3]:=StrToInt(Edit3.Text);
mergesort(a, 3);
for i:=1 to 3do
begin
Memo1.Lines.Add(IntToStr(a));
end;
end;
end.
procedure merge(x:array of Integer;
s:array of Integer;
l:Integer;
m:Integer;
n:Integer);
var
i,j,k:Integer;
begin
i:=l;
j:=m+1;
k:=l;
while (i<=m) and (j<=n)do
begin
if (x< x[j]) then
begin
s[k]:=x;
i:=i+1;
k:=k+1;
end
else
begin
s[k]:=x[j];
j:=j+1;
k:=k+1;
end;
end;
while (i<=m)do
begin
s[k+1]:=x[i+1];
end;
while (j<=n)do
begin
s[k+1]:=x[j+1];
end;
end;
//
procedure mergpass(x:array of Integer;
s:array of Integer;
n:Integer;
l:Integer);
var
i,j:Integer;
begin
i:=1;
while(i+2*l-1=n)do
begin
merge(x,s,i,i+l-1,i+2*l-1);
i:=i+2*l;
end;
if (i+l-1<=n) then
begin
merge(x,s,i,i+l-1,n);
end
else
begin
for j:=i to ndo
begin
s[j]:=x[j];
end;
end;
end;
//
procedure mergesort(r:array of Integer;
n:Integer);
var
i,len:Integer;
s:array[1..3] of Integer;
begin
len:=1;
while(len<n)do
begin
mergpass(r,s,n,len);
for i:=1 to ndo
begin
r:=s;
len:=2*len;
end;
end;
end;
//
procedure TForm1.Button1Click(Sender: TObject);
var
i:Integer;
a:array[1..3] of Integer;
begin
a[1]:=StrToInt(Edit1.Text);
a[2]:=StrToInt(Edit2.Text);
a[3]:=StrToInt(Edit3.Text);
mergesort(a, 3);
for i:=1 to 3do
begin
Memo1.Lines.Add(IntToStr(a));
end;
end;
end.