This Post Contains A C++ Program To Perform Heap Sort With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At TurboC/C++3.0 Compiler & Will Help You To Understand The Concept Of 'Heap Sort' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.
# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.
C++ Program To Perform Heap Sort.
/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>
void hsort(int x[], int n);
/* Start Of Main Program */
void main()
{
/* Declaration Of Variables */
int i, dim;
int a[20];
clrscr();
/* Asking For The Input From User */
cout<<" \n Enter Dimension : ";
cin>>dim;
cout<<" \n Enter Values For Array : ";
for(i=0;i<dim;i++)
{
cin>>a[i];
}
cout<<" \n Entered Values Of Array Are : ";
for(i=0;i<dim;i++)
{
cout<<a[i]<<"\t";
}
hsort(a, dim); // Call To The Function.
cout<<" \n After Heap Sort : ";
for(i=0;i<dim;i++)
{
cout<<a[i]<<"\t";
}
getch();
}
/* End Of Main Program */
/* Source Code For Computing Heap Sort */
/* Start Of Heap Sort Function */
void hsort(int x[], int n)
{
int i, s, f, elt, ivalue;
for(i=1;i<n;i++)
{
elt=x[i];
s=i;
f=(s-1)/2;
while(s>0 && x[f]<elt)
{
x[s]=x[f];
s=f;
f=(s-1)/2;
}
x[s]=elt;
}
for(i=n-1;i>0;i--)
{
ivalue=x[i];
x[i]=x[0];
f=0;
if(i==1)
{
s=-1;
}
else
{
s=1;
}
if( (i>2) && (x[2]>x[1]) )
{
s=2;
}
while( (s>=0) && (ivalue<x[s]) )
{
x[f]=x[s];
f=s;
s=2*f+1;
if( (s+1<=i-1) && (x[s]<x[s+1]) )
s=s+1;
if(s>i-1)
s=-1;
}
x[f]=ivalue;
}
}
/* End Of Heap Sort Function */
Output :
Enter Dimension : 5
Enter Values For Array :
3 1 5 2 4
After Heap Sort :
1 2 3 4 5
# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.
C++ Program To Perform Heap Sort.
/* Declaration Of Header Files */
#include<iostream.h>
#include<conio.h>
void hsort(int x[], int n);
/* Start Of Main Program */
void main()
{
/* Declaration Of Variables */
int i, dim;
int a[20];
clrscr();
/* Asking For The Input From User */
cout<<" \n Enter Dimension : ";
cin>>dim;
cout<<" \n Enter Values For Array : ";
for(i=0;i<dim;i++)
{
cin>>a[i];
}
cout<<" \n Entered Values Of Array Are : ";
for(i=0;i<dim;i++)
{
cout<<a[i]<<"\t";
}
hsort(a, dim); // Call To The Function.
cout<<" \n After Heap Sort : ";
for(i=0;i<dim;i++)
{
cout<<a[i]<<"\t";
}
getch();
}
/* End Of Main Program */
/* Source Code For Computing Heap Sort */
/* Start Of Heap Sort Function */
void hsort(int x[], int n)
{
int i, s, f, elt, ivalue;
for(i=1;i<n;i++)
{
elt=x[i];
s=i;
f=(s-1)/2;
while(s>0 && x[f]<elt)
{
x[s]=x[f];
s=f;
f=(s-1)/2;
}
x[s]=elt;
}
for(i=n-1;i>0;i--)
{
ivalue=x[i];
x[i]=x[0];
f=0;
if(i==1)
{
s=-1;
}
else
{
s=1;
}
if( (i>2) && (x[2]>x[1]) )
{
s=2;
}
while( (s>=0) && (ivalue<x[s]) )
{
x[f]=x[s];
f=s;
s=2*f+1;
if( (s+1<=i-1) && (x[s]<x[s+1]) )
s=s+1;
if(s>i-1)
s=-1;
}
x[f]=ivalue;
}
}
/* End Of Heap Sort Function */
Output :
Enter Dimension : 5
Enter Values For Array :
3 1 5 2 4
After Heap Sort :
1 2 3 4 5
No comments:
Post a Comment