用户ID: 密码: 验证:

登 录

注 册 取回密码

中山教育

中山国际网

中国教育在线

时代财富科技公司 FortuneAge Technology Co., Ltd. 校园博客客服网站(新)

我的资料

Mpq

博客信息

积分:988
等级:4级 lv 4
日志总数:231
发表评论总数:18 ( 查看)
获得评论总数:21
发表留言总数:0
所属学校:三鑫
收藏本站:

最新公告

欢迎光临我的博客!

最新相册

我的日历

最新评论

--游客
好文好文,是您的手笔?如果是您的文章,如果您愿意和我联系,...
还有就是数学不能使用计算器…….......在读初中之前原...
--Mpq
这次似乎不举行冬季长跑……看来在三鑫参加的体育项目最终是以...
--Mpq
看了你的"总结", 挺有针对性的! 相信下学期你一定会有...

RSS


首页 -> 经典算法->HeapSort
HeapSort

Program Heap_Sort_Mpq_2007_1_31;

const Maxn=1000000;
type data=array[1..Maxn] of longint;
var
  a:data;
  i,n,Tmp:longint;

procedure heap(n,i:longint);
var
  x,j:longint;
begin
  x:=a[i];
  j:=2*i;
  while j<=n do
  begin
    if (j<n) and (a[j]<a[j+1]) then j:=j+1;
    if x<a[j] then
    begin
      a[i]:=a[j];
      i:=j;
      j:=j*2;
    end else break;
  end;
  a[i]:=x;
end;

begin
assign(input,'sort.in'); reset(input);
assign(output,'sort.out'); rewrite(output);
  readln(n);
  for i:=1 to n do readln(a[i]);
  for i:=n div 2 downto 1 do
    heap(n,i);
  for i:=n downto 2 do
  begin
    Tmp:=a[1];
    a[1]:=a[i];
    a[i]:=Tmp;
    heap(i-1,1);
  end;
  for i:=1 to n do writeln(a[i]);
close(input); close(output);
end.

网友评论

共 0 页,0 条记录  

用户名:
密码:
您的评论:
正在载入编辑器...
请输入验证码:


发 表 评 论

Mpq-中山教师家园