主题:p
pupu
[专家分:50] 发布于 2008-08-25 14:50:00
堆排序.
最后更新于:2008-08-25 14:59:00
回复列表 (共1个回复)
沙发
无所不能 [专家分:270] 发布于 2008-08-25 18:23:00
program lx;
const
n:=100;
var
a:array[1..100] of integer;
i,x:integer;
procedure aa(i,m:integer);
var
x:integer;
begin
while i*2<=m do
begin
i:=i*2;
if (i<m) and (a[i+1]>a[i]) then inc(i);
if a[i]>a[i div 2] then
begin
x:=a[i div 2];
a[i div 2 ]:=a[i];
a[i]:=x;
end;
else break;
end;
end;
begin
randomize;
for i:=1 to n do
a[i]:=random(100);
for i;=n div 2 downto 1 do
aaa(i,n);
for i:=n div 2 downto 2 do
begin
x:=a[i];
a[i]:=a[1];
a[1]:=x;
aaa(1,i-1);
end;
for i:=1 to n do
writeln(a[i]);
end.
如果有错的自己调,我没调!
我来回复