Heap sort

// Heap sort //

#include"iostream.h'
#include"conio.h"
#define SIZE 30
int main()
{
int a[SIZE],heap[SIZE];
int i,n,count,temp;
void makeheap(int a[],int);
void heapsort(int a[],int);
clrscr();
cout<<"Enter the no. of terms:";
cin>>n;
cout<<"Enter the list:\n";
for(i=0;icin>>a[i];
makeheap(a,n);
cout<<"\nHeap is:\n";
for(i=0;i < n;i++)
cout << a[i] << endl;
getch();
return(0);
}
void makeheap(int a[],int n)
{
int i,son,fat,value;
for(i=1;i{
value=a[i];
son=i;
fat=(son-1)/2;
while(son>0 && a[fat]< value)
{
a[son]=a[fat];
son=fat;
fat=(son-1)/2;
}
a[son]=value;
}
return;
}

Post a Comment

Previous Post Next Post