Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5707952
  • 博文数量: 675
  • 博客积分: 20301
  • 博客等级: 上将
  • 技术积分: 7671
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-31 16:15
文章分类

全部博文(675)

文章存档

2012年(1)

2011年(20)

2010年(14)

2009年(63)

2008年(118)

2007年(141)

2006年(318)

分类:

2006-09-03 15:13:06

#include
using namespace std;

void MakeHeap (int a[], int n);

int
main ()
{
  int a[100];
  int n, i;

  cout << "Enter the number:" << endl;
  cin >> n;

  for (i = 1; i <= n; i++)
    {
      cout << "Enter the element of the Heap:" << endl;
      cin >> a[i];
    }

  MakeHeap (a, n);

  cout << "The Heap is:" << endl;
  for (i = 1; i <= n; i++)
    {
      cout << a[i] << " ";
    }

  cout << endl;

  return 1;
}

void
MakeHeap (int a[], int n)
{
  int i, j, flag;

  for (i = n / 2; i >= 1; i--)
    {
      j = 2 * i;
      if (a[j] <= a[j + 1])
    j++;
      if (a[i] < a[j])
    {
      flag = a[i];
      a[i] = a[j];
      a[j] = flag;
    }
    }
}

阅读(2808) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~