Advertisement
Guest User

Heap

a guest
Oct 16th, 2011
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 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.   for (i=n/2;i>=1;i--)
  8.   {
  9.   int k=i;
  10.   int v=h[k];
  11.   bool heap=false;
  12.        while(!heap&&2*k<=n)
  13.        {
  14. cout<<"\n i value is :"<<i;
  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.        h[k]=v;          
  30.        }//end of while
  31.    
  32.      }
  33. cout<<"\n HEAP GENERATED \n";
  34. for(int i=0;i<n;i++)
  35. cout<<"\n ELEMENT IS:"<<h[i];
  36. }
  37. int main()
  38. {
  39.  
  40. cout<<"\n Enter the maximum number of array elements \n";
  41. cin>>n;
  42. cout<<"\n Enter the array to perform heap sort \n";
  43. for(int i=0;i<n;i++)
  44. cin>>h[i];
  45.  
  46. heapbottom(h,n);
  47.  
  48.  
  49. return 0;
  50. }
  51.  
  52.  
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement