Logo 
Search:

C++ Programming Articles

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

Program of heap sort

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

Write a program of heap sort.

Code for Program of heap sort in C++ Programming

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

void main()
{
 clrscr();
 int a[50],size;
 int p,c;
 cout<<"Enter the Size of an Array :";
 cin>>size;

 cout<<"Enter Value of A[1] :";
 cin>>a[1];
 for(int i=2;i<=size;i++)
 {
    cout<<"Enter Value of A["<<i<<"] :";
    cin>>a[i];
    p=i/2;
    c=i;
    while(1)
    {
    if( a[c] > a[p])
    {
        int t=a[c];
        a[c]=a[p];
        a[p]=t;
    }
    c=p;
    p=p/2;
    if(p<1)
    {
        break;
    }
    }
 }
 cout<<endl<<"Heap ..."<<endl;
 for(i=1;i<=size;i++)
 {
    cout<<endl;
    cout<<"Arr["<<i<<"] :"<<a[i];
 }

 int j=size;
 int lc,rc;
 while(j>1)
 {
     if(a[1] > a[j])
     {
    int t=a[1];
    a[1]=a[j];
    a[j]=t;
    j--;
     }
     else
     {
    j--;
    continue;
     }

     p=1;
     while(p < j)
     {
     lc=p*2;
     rc=p*2 + 1;
     if(lc>=j || rc >=j)
     {
            break;
     }
     if(a[p] < a[lc] && a[lc] > a[rc])
     {
    int temp=a[lc];
    a[lc]=a[p];
    a[p]=temp;
    p=lc;
     }
     elseif (a[p] < a[rc] && a[rc] > a[lc])
     {
    int temp=a[rc];
    a[rc]=a[p];
    a[p]=temp;
    p=rc;
     }
     else
     {
      break;
     }
     }
 }

 cout<<endl<<"Sorted List ..."<<endl;
 for(i=1;i<=size;i++)
 {
    cout<<endl;
    cout<<"Arr["<<i<<"] :"<<a[i];
 }

 getch();
}
  
Share: 


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

Easy Tutor
Easy Tutor author of Program of 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].

 
Bryon Segunto from Philippines Comment on: May 23
why the sorted list is 1,4,3,5,6 it should be 1,3,4,5,6, explain it to me please :D

View All Comments