Heap Sort (min heap)

Sorting Using min heap and arrange in decending order 


 Other Type Of Sorting : Click here :: most important ::


#include<iostream.h>
#include<conio.h>
#define max 7
class array{
int arr[max];
int count;
public:
array()
{
count=0;
for(int i=0;i<max;i++)
{ arr[i]=0; }
}
void add(int num){
if(count<max){
arr[count]=num; count++;}
else
{cout<<"\n Array is full"<<endl;}
}
void heapsort()
{
for(int i=count-1;i>0;i--)
{int ivalue=arr[i];
arr[i]=arr[0];
arr[0]=ivalue;
makeheap(i);}
}
void display()
{
for(int i=0;i<count;i++)
{ cout<<arr[i]<<"\t";}
}
void makeheap(int c)
{
for(int i=0;i<c;i++)
{
 int val=arr[i];
int s=i;
int f=(s-1)/2;
while(s>0 && arr[f]>val)
{ arr[s]=arr[f];
s=f;
f=(s-1)/2;}
arr[s]=val;
}
}
};
void main()
{
clrscr();
array a;
a.add(15);
a.add(19);
a.add(18);
a.add(7);
a.add(17);
a.add(16);
a.add(8);
cout<<"\n\n Values you insert in min order heap\n \n";
a.display();
a.makeheap(7);
cout<<"\n\n Values inserted in Heap tree\n\n";
a.display();
a.heapsort();
cout<<"\n\n Values after heap sort (min order)( desecnding )\n\n";
a.display();
getch();
}



No comments

Powered by Blogger.