**Write a C program to implement Heap Sort algorithm.**

**Program implementation of above problem:***#include <stdio.h>*

*#include <conio.h>*

*int p(int i)*

*{*

*return i/2;*

*}*

*int left(int i)*

*{*

*return 2*i+1;*

*}*

*int right(int i)*

*{*

*return 2*i+2;*

*}*

*void heapify(int a[],int i,int n)*

*{*

*int l,r,large,t;*

*l=left(i);*

*r=right(i);*

*if((l<=n-1)&&(a[l]>a[i]))*

*large=l; else large=i;*

*if((r<=n-1)&&(a[r]>a[large]))*

*large=r;*

*if(large!=i)*

*{*

*t=a[i];*

*a[i]=a[large];*

*a[large]=t;*

*heapify(a,large,n);*

*}*

*}*

*void buildheap(int a[],int n)*

*{*

*int i;*

*for(i=(n-1)/2;i>=0;i--)*

*heapify(a,i,n);*

*}*

*void heapsort(int a[],int n)*

*{*

*int i,m,t;*

*buildheap(a,n);*

*m=n ;*

*for(i=n-1;i>=1;i--)*

*{*

*t=a[0];*

*a[0]=a[i];*

*a[i]=t;*

*m=m-1;*

*heapify(a,0,m);*

*}*

*}*

*void main()*

*{*

*int x[20],n,i;*

*clrscr();*

*printf("\n\nPlease enter the number of elements to be sorted : ");*

*scanf("%d",&n);*

*printf("\n\nPlease enter %d integer elements : ",n);*

*for(i=0;i<n;i++)*

*scanf("%d",&x[i]);*

*heapsort(x,n);*

*printf("\n\nList of elements after sort : ");*

*for(i=0;i<n;i++)*

*printf("%d ",x[i]);*

*getch();*

*}*

**Thanks**

**Mukesh Rajput**
## Post A Comment:

## 0 comments:

Thanks

Mukesh Rajput