title: build heap procedure
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/7823524/build-heap-procedure-crash-during-its-worlking">build heap procedure crash during it&#39;s worlking</a>  </p>
  </blockquote>
  
  
  
  <pre><code> #include&lt;iostream&gt;
  #include&lt;math.h&gt;
  using namespace std;
  #define  maxn 1000
  int x[maxn];
  
  int parent(int i){
      return int(i/2);
  
  }
  int left(int i){
      return 2*i;
  
  }
  int right(int i){
      return 2*i+1;
  
  }
  void  max_heap(int x[],int i,int size){
       bool s=true;
       int largest;
        for (int k=1;k&lt;size/2;k++){
            if(x[k]&lt; x[2*k] ||  x[k]&lt;x[2*k+1]){
                s=false;
            }
  
        }
         if (s==true) return;
         else{
  
      if(i&gt;=size) return ;
  
      int l=left(i);
      int r=right(i);
  
      if (l&lt;=size &amp;&amp;  x[l]&gt;x[i]){
          largest=l;
      }
      else
      {
          largest=i;
      }
      if (r&lt;=size &amp;&amp; x[r]&gt;x[largest]){
      largest=r;
      }
      if (largest!=i)  { int s=x[i];x[i]=x[largest];x[largest]=s;}
         }
      max_heap(x,largest,size);
  }
   void build(int x[],int size){
       int heapsize=size;
        for (int i=(size/2);i&gt;1;i--)
            max_heap(x,i,size);
  
  
   }
  
  
  
  int main(){
  
      x[1]=4;
      x[2]=1;
      x[3]=3;
      x[4]=2;
      x[5]=16;
      x[6]=9;
      x[7]=10;
      x[8]=14;
      x[9]=8;
      x[10]=7;
      build(x,10);
       for (int i=1;i&lt;=10;i++)
           cout&lt;&lt;x[i]&lt;&lt;"  ";
  
  
  
  
  
  
      return 0;
  }
  </code></pre>
  
  <p>this code fails,while only max_heap  procedure works find,please tell me what is wrong in code?i have spent hours but did not find something wrong yet</p>