title: How to implement a max heap
body: <p>I have the code to build a max heap, but it keeps on returning the same array I give it. I'm sure its a minor error, but I cant seem to figure it out. Any help is appreciated.</p>
  
  <p>Compilable sample code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cmath&gt;
  
  class Heaparr {
  public:
      Heaparr();
      void insert(int da);
      int getLeft(int i) { return 2 * i; }
      int getRight(int i) { return (2 * i) + 1; }
      int getParent(int i) { return i / 2; }
      int getMax() { return maxHeap[0]; }
      void print();
      void reheap(int num);
      void makeArray();
      void Build_Max_Heap(int maxHeap[], int heap_size);
      void Max_Heapify(int heapArray[], int i, int heap_size);
      void heapSort(int heapArray[]);
  
  private:
      int size;
      int* maxHeap;
      int index;
      int i;
  };
  
  Heaparr::Heaparr() {
      maxHeap = nullptr;
      size = 0;
  }
  
  void Heaparr::insert(int da) {
      size++;
      int* tmp = new int[size];
  
      for (int i = 0; i &lt; size - 1; i++) {
          tmp[i] = maxHeap[i];
      }
  
      tmp[size - 1] = da;
      delete[] maxHeap;
      maxHeap = tmp;
  }
  
  void Heaparr::heapSort(int maxHeap[]) {
      int heap_size = size;
      int n = size;
      int temp;
  
      Build_Max_Heap(maxHeap, heap_size);
  
      for (int i = n - 1; i &gt;= 1; i--) {
          temp = maxHeap[0];
          maxHeap[0] = maxHeap[i];
          maxHeap[i] = temp;
  
          heap_size = heap_size - 1;
          Max_Heapify(maxHeap, 0, heap_size);
      }
  
      for (int i = 0; i &lt; 8; i++) {
          std::cout &lt;&lt; maxHeap[i] &lt;&lt; std::endl;
      }
  }
  
  void Heaparr::Build_Max_Heap(int maxHeap[], int heap_size) {
      int n = size;
      for (int i = floor((n - 1) / 2); i &gt;= 0; i--) {
          Max_Heapify(maxHeap, i, heap_size);
      }
      return;
  }
  
  void Heaparr::Max_Heapify(int heapArray[], int i, int heap_size) {
      // int n = size;
      int largest = 0;
      int l = getLeft(i);
      int r = getRight(i);
  
      if ((l &lt;= heap_size) &amp;&amp; (heapArray[l] &gt; heapArray[i])) {
          largest = l;
      } else {
          largest = i;
      }
  
      if ((r &lt;= heap_size) &amp;&amp; (heapArray[r] &gt; heapArray[largest])) {
          largest = r;
      }
  
      int temp;
      if (largest != i) {
          temp = heapArray[i];
          heapArray[i] = heapArray[largest];
          heapArray[largest] = temp;
  
          Max_Heapify(heapArray, largest, heap_size);
      }
      return;
  }
  
  int main(int argc, char* argv[]) {
      int hArray[8] = {5, 99, 32, 4, 1, 12, 15, 8};
      Heaparr t;
      t.heapSort(hArray);
      for (auto v : hArray) {
          std::cout &lt;&lt; v &lt;&lt; ", ";
      }
      std::cout &lt;&lt; std::endl;
  }
  </code></pre>