title: Build Max Heap algorithm won't work
body: <p>I am trying to build a max heap using the algorithm given in Introduction to Algorithms, and I can't get it to work. Firstly, I'm having some interesting issues with passing arrays between functions. My understanding is that arrays are passed by reference in C++, but after I pass an array and I check the addresses of the argument and the parameter they don't match. When I try to use sizeof() on an array that is a parameter it doesn't return the correct value. </p>
  
  <p>Secondly, the algorithm just doesn't seem to work. It does make some changes to the array, and gets the numbers closer to a heap but still has a ways to go. I've checked the code dozens of times and it seems to be spot on with pseudo code given in my text. What am I missing? </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;ctime&gt;
  
  #define N 10
  #define DISPLAY
  
  using namespace std;
  
  
  // Display an array
  void display(double p[]) {
  #ifdef DISPLAY
      for (int i = 0; i &lt; N; i++) cout &lt;&lt; p[i] &lt;&lt; " ";
      cout &lt;&lt; endl;
  #endif
  }
  
  
  inline int parent(int i)
  {return i/2;}
  
  inline int left(int i)
  {return 2*i;}
  
  inline int right(int i)
  {return 2*i+1;}
  
  void maxHeapify(double p[], int i)
  {
      int largest;
  
     int l = left(i);
     int r = right(i);
  
     if (l &lt;= N &amp;&amp; (p[l] &gt; p[i]))
          largest = l;
  
      else
          largest = i;
  
      if (r &lt;= N &amp;&amp; (p[r] &gt; p[largest]))
          largest = r;
  
      if (largest != i)
      {
          double temp = p[i];
  
          p[i] = p[largest];
  
          p[largest] = temp;
  
          maxHeapify(p, largest);
      }
  }   
  
  void buildMaxHeap(double p[])
  {
      for (int i=N/2; i&gt;0; i--)
          maxHeapify(p, i);
  }
  
  int main() {
   double a[] = {4,1,3,2,16,9,10,14,8,7};
   buildMaxHeap(a);
   display(a);
  }
  </code></pre>