按算法导论改写的堆排序,有错误,麻烦大家帮忙更正一下,谢谢
[code=C/C++][/code]#include <conio.h>
#include <iostream.h>
int PARENT(int i)
{
return i/2;
}
int LEFT(int i)
{
return 2*i;
}
int RIGHT(int i)
{
return 2*i+1;
}
void MAX_HEAPIFY(int *A,int i,int heap_size) //调整堆节点
{
int largest=i;
int temp;
int l=LEFT(i);
int r=RIGHT(i);
if(i<=heap_size&&A[l]>A[i])
largest=l;
if(i<=heap_size&&A[r]>A[i])
largest=r;
if(largest!=i)
{
temp=A[i];
A[i]=A[largest];
A[largest]=temp;
MAX_HEAPIFY(A,largest,heap_size);
}
}
void BUILD_MAX_HEAP(int *A,int heap_size) //建堆
{
int i;
for(i=heap_size/2;i>=1;i--)
MAX_HEAPIFY(A,i,heap_size);
}
void HEAPSORT(int *A,int heap_size) //堆排序
{
int i;
BUILD_MAX_HEAP(A,heap_size);
int temp;
for(i=heap_size;i>=1;i--)
{
temp=A[1];
A[1]=A[i];
A[i]=temp;
MAX_HEAPIFY(A,1,heap_size);
}
}
int main()
{
int a[100];
int size;
while(scanf("%d",&size)==1&&size>0)
{
int i;
for(i=1;i<=size;i++)
cin>>a[i];
HEAPSORT(a,size);
for(i=1;i<=size;i++)
cout<<a[i]<<" ";
cout<<endl;
}
return 0;
}
//运行环境:VC6
结果有错误,帮忙检查一下
[解决办法]
http://topic.csdn.net/u/20120119/00/ad2eb3ab-4b9e-4dc0-b4c5-609b7be5e641.html?55975