Guest User

Untitled

a guest
Jan 23rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. int h[10],n;
  4. void heapbottom(int h[],int n)
  5. {
  6. int i,j;
  7.  
  8.   for (i=n/2;i>=1;i--)
  9.   {
  10.   int k=i;
  11.   int v=h[k];
  12.   bool heap=false;
  13.        while(!heap&&2*k<=n)
  14.        {
  15.            j=2*k;
  16.            if(j<n)//there sre 2 children
  17.             {
  18.              if(h[j]<h[j+1])
  19.              j++;
  20.             }
  21.            if(v>=h[j])
  22.             heap=true;
  23.            else
  24.              {
  25.              h[k]=h[j];
  26.              k=j;
  27.              }
  28.              
  29.                  
  30.        }//end of while
  31.         h[k]=v;
  32.  
  33.      }
  34. cout<<"\n HEAP GENERATED \n";
  35. for(int i=0;i<n;i++)
  36. cout<<"\n ELEMENT IS:"<<h[i];
  37. }
  38. int main()
  39. {
  40.  
  41. cout<<"\n Enter the maximum number of array elements \n";
  42. cin>>n;
  43. cout<<"\n Enter the array to perform heap sort \n";
  44. for(int i=0;i<n;i++)
  45. cin>>h[i];
  46.  
  47. heapbottom(h,n);
  48.  
  49.  
  50. return 0;
  51. }
Add Comment
Please, Sign In to add comment