C++ program to implement Heap sort Algorithm

#include <iostream.h>
const int MAX = 10 ;
class array
{
private :
int arr[MAX] ;
int count ;
public :
array( ) ;
void add ( int num ) ;
void makeheap(int ) ;
void heapsort( ) ;
void display( ) ;
} ;
array :: array( )
{
count = 0 ;
for ( int i = 0 ; i < MAX ; i++ )
arr[MAX] = 0 ;
}
void array :: add ( int num )
{
if ( count < MAX )
{
arr[count] = num ;
count++ ;
}
else
cout << "\nArray is full" << endl ;
}
void array :: makeheap(int c)
{
 
for ( int i = 1 ; 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 array :: heapsort( )
{
for ( int i = count - 1 ; i > 0 ; i-- )
{
int ivalue = arr[i] ;
arr[i] = arr[0] ;
arr[0]=ivalue;
makeheap(i);
 
}
}
void array :: display( )
{
for ( int i = 0 ; i < count ; i++ )
cout << arr[i] << "\t" ;
cout << endl ;
}
void main( )
{
array a ;
 
a.add ( 11 ) ;
a.add ( 2 ) ;
a.add ( 9 ) ;
a.add ( 13 ) ;
a.add ( 57 ) ;
a.add ( 25 ) ;
a.add ( 17 ) ;
a.add ( 1 ) ;
a.add ( 90 ) ;
a.add ( 3  ) ;
a.makeheap(10) ;
cout << "\nHeap Sort.\n" ;
cout << "\nBefore Sorting:\n"  ;
a.display( ) ;
a.heapsort( ) ;
cout << "\nAfter Sorting:\n" ;
a.display( ) ;
}
Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

13 thoughts on “C++ program to implement Heap sort Algorithm

  1. It’s output is just the same with those in the selection sort. Does the code also works for the selection sort or only for heap sort?

  2. Correct me if I am wrong, instead of

    while ( s > 0 && arr[f] 0 && arr[f] < val )
    {
    arr[s] = arr[f] ;
    s = f ;
    f = ( s – 1 ) / 2 ;
    }
    arr[s] = val ;
    void array :: makeheap(int c)
    {

    for ( int i = 1 ; i 0 && arr[f] < val )
    {
    swap(arr, s, f)
    s = f ;
    f = ( s – 1 ) / 2 ;
    }
    }
    }

  3. write a program for priority queue. where all the property will satisfy that means maximum, heap increase key, insertition, deletion , return max operation should present?

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in