Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Data File StructureRSS Feeds

Program to perform heap sort

Posted By: Easy Tutor     Category: C++ Programming     Views: 13260

Write a Program to perform heap sort.

Code for Program to perform heap sort in C++ Programming

//Heap sort

#include <iostream.h>
#include <conio.h>

class heap
{
int k[11],size;
public:
void getdata(void);
friend void create_heap(heap &);
friend void sort_heap(heap &);
void showdata(void);
};

void heap :: getdata(void)
{
clrscr();
cout<<"Enter the size of Array :-";
cin>>size;
cout<<"\nEnter "<<size<<" Elements\n";
 for(int i=1;i<=size;i++) //Creating heap from  index1 instead of index0
   cin>>k[i];
}

void heap :: showdata(void)
{
  clrscr();
  cout<<"\n\nHeap Function Output\n\n";
  for(int i=1;i<=size;i++)
    cout<<k[i]<<endl;
}


void create_heap(heap &a)
{
int q,i,j,key;
  for(q=2;q<=a.size;q++)
  {
    i=q;
    key=a.k[i];
    j=i/2;
    while(i>1 && key>a.k[j])
      {
         a.k[i]=a.k[j];
         i=j;
         j=i/2;
         if(j<1)
            j=1;
      }
    a.k[i]=key;
    }
}

void sort_heap(heap &a)
{
int q,i,j,key,temp;
  for(q=a.size;q>=1;q--)
  {
     temp=a.k[1];
     a.k[1]=a.k[q];
     a.k[q]=temp;
     i=1;
     key=a.k[i];
     j=2*i;
     if(j+1 < q)
     {
         if(a.k[j+1] > a.k[j])
            j++;
        }
     while(j<=q-1 && key<a.k[j])
     {
       a.k[i]=a.k[j];
       i=j;
       j=2*i;
       if(j+1 < q)
       {
          if(a.k[j+1] > a.k[j])
             j++;
       }
       elsebreak;
     }
     a.k[i]=key;
    }
}

void main()
{
heap o1;
o1.getdata();
create_heap(o1);
sort_heap(o1);
o1.showdata();
getch();
}
  
Share: 


Didn't find what you were looking for? Find more on Program to perform heap sort Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program to perform heap sort is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!