You are on page 1of 2

/*program for heap sort*/

#include<stdio.h>
#include<stdlib.h>
void buildheap(int a[10],int i,int n)
{
int temp,c;
temp=a[i];
c=2*i;
while(c<=n)
{
if(c<n&&a[c+1]>a[c])
c++;
if(temp<a[c])
{
a[c/2]=a[c];
}
else
break;
c=2*c;
}
a[c/2]=temp;
}
void swap(int a[10],int i,int j)
{
int t;
t=a[i];
a[i]=a[j];
a[j]=t;
}
void heapsort(int a[10],int n)
{
int i;
for(i=n/2;i>=1;i--)
buildheap(a,i,n);
for(i=n;i>=2;i--)
{
swap(a,1,i);
buildheap(a,1,i-1);
}
}
void display(int a[10],int n)
{
int i;
for(i=1;i<=n;i++)
printf("%d\t",a[i]);
}
main()
{
int a[10],n,i;
printf("enter the value of n\n");
scanf("%d",&n);
printf("enter the elements\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("\nafter sorting\n");
heapsort(a,n);

display(a,n);
}

You might also like