|
CPP Program To Implement Heap Sort: |
|
#include
#include
int hsort[25],n,i;
void adjust(int,int);
void heapify();
void main()
{
int temp;
clrscr();
cout<<"\n\t\t\t\tHEAP SORT";
cout<<"\n\t\t\t\t**** ****\n\n\n";
cout<<"\nenter no of elements:";
cin>>n;
cout<<"\nenter elements to be sorted\n\n";
for(i=1;i<=n;i++)
cin>>hsort[i];
heapify();
for(i=n;i>=2;i--)
{
temp=hsort[1];
hsort[1]=hsort[i];
hsort[i]=temp;
adjust(1,i-1);
}
cout<<"\nSORTED ELEMENT\n\n";
for(i=1;i<=n;i++)
cout<=1;i--)
adjust(i,n);
}
void adjust(int i,int n)
{
int j,element;
j=2*i;
element=hsort[i];
while(j<=n)
{
if((j=hsort[j])
break;
hsort[j/2]=hsort[j];
j=2*j;
}
hsort[j/2]=element;
}
SAMPLE INPUT AND OUTPUT:
Enter No Of Elements:5
Enter Elements To Be Sorted
5
4
3
2
1
Sorted Element
1
2
3
4
5
|
|
⇓ Student Projects ⇓
⇑ Student Projects ⇑ |