Mega Code Archive
A program to implement Heap Sort
#include
void restoreHup(int*,int);
void restoreHdown(int*,int,int);
void main()
{
int a[20],n,i,j,k;
printf("
Enter the number of elements to sort : ");
scanf("%d",&n);
printf("
Enter the elements :
");
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
restoreHup(a,i);
}
j=n;
for(i=1;i<=j;i++)
{
int temp;
temp=a[1];
a[1]=a[n];
a[n]=temp;
n--;
restoreHdown(a,1,n);
}
n=j;
printf("
Here is it...
");
for(i=1;i<=n;i++)
printf("%4d",a[i]);
}
void restoreHup(int *a,int i)
{
int v=a[i];
while((i>1)&&(a[i/2]