# Heapsort_ unable to execute_Error: Segmentation_Fault

#include<stdio.h>
//heapsort
void heapsort(int a[], int);
void heapify(int a[], int ,int);
void buildheap(int a[],int);
int main()
{
int i,j,n,a[33];
printf(“Enter the total number for sorting\n”);
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf(“Enter the number in array\n”);
scanf("%d", &a[i]);
}
heapsort(a,n);
}
void heapsort(int a[], int n)
{
buildheap(a,n);
int heapsize,i,temp;
heapsize=n-1;
for(i=heapsize;i>=0;i–)
{
temp=a[0];
a[0]=a[heapsize];
a[heapsize]=temp;
heapsize–;
heapify(a,0,heapsize);
}
for(i=0;i<n;i++)
{
printf("%d", a[i]);
}
}
void buildheap(int a[33], int n)
{
int heapsize;
heapsize=n-1;
for(int i=n/2;i>=0;i–)
{
heapify(a,i,heapsize);
}
}
void heapify(int a[33],int i,int heapsize)
{

``````	int l,r, largest, temp;
l=2*i;
r=2*i+1;
if(l<= heapsize && a[l]>a[i])
largest=l;
else
largest=i;
if(r<= heapsize && a[r]>a[i])
largest=r;
if(largest!=i)
{
temp=a[i];
a[i]=a[largest];
a[largest]=temp;
}
heapify(a,largest,heapsize);
}``````