#include<stdio.h>
int heapsize,arrlength;
void MinHeapify(int arr[],int i)
{
int largest,temp;
int l=2*i+1;
int r=2*i+2;
if(l<=heapsize&&arr[l]>arr[i])
largest=l;
else
largest=i;
if(r<=heapsize &&arr[r]>arr[largest])
largest=r;
if(largest!=i)
{
temp=arr[i];
arr[i]=arr[largest];
arr[largest]=temp;
MinHeapify(arr,largest);
}
}
void buildMinHeap(int arr[])
{
heapsize=arrlength-1;
for(int i=(arrlength/2)-1;i>=0;i--)
MinHeapify(arr,i);
}
void heapSort(int arr[])
{
int temp;
buildMinHeap(arr);
for(int i=arrlength-1;i>=0;i--)
{
temp=arr[0];
arr[0]=arr[i];
arr[i]=temp;
heapsize=heapsize-1;
MinHeapify(arr,0);
}
}
int main()
{
int arr[]={1,12,9,5,6,10};
arrlength=6;
printf("\nArray before sorting is:");
for(int i=0;i<arrlength;i++)
printf("%d ",arr[i]);
heapSort(arr);
printf("\nArray after sorting is:");
for(int i=0;i<arrlength;i++)
printf("%d ",arr[i]);
}