title: Binary Max Heap Problem for two different results
body: <p>I am trying to learn about binary heaps and I tried to implement a max binary heap. Following is the code I wrote.</p>
  <pre><code>public class MaxBinaryHeap {
      
      private int size;
      
      int[] heap;
      
      
  
      public MaxBinaryHeap(int size) {
          
          this.size = size;
      }
  
  
  
      public static void main(String[] args) {
          //creating a heap
          MaxBinaryHeap binaryHeap = new MaxBinaryHeap(20);
          
          //adding elements to heap array
          binaryHeap.add(2);
          binaryHeap.add(7);
          binaryHeap.add(26);
          binaryHeap.add(25);
          binaryHeap.add(19);
          binaryHeap.add(17);
          binaryHeap.add(1);
          binaryHeap.add(90);
          binaryHeap.add(3);
          binaryHeap.add(36);
          
          //calling this method to reorganize the heap array
          binaryHeap.heap();
          
      }
  
  
  
      private void heap() {
          int size = heap.length;
          System.out.println(Arrays.toString(heap));
          for(int i=0;i&lt;size;i++) {
                //following is call to another heap method - commented
              //heapify2(heap)
  
              heapify(heap);
          }
          
      }
  
  
      //method to add elements in heap array
      private void add(int i) {
          
          if(heap==null) {
              heap = new int[1];
              heap[0]=i;
          }
          else {
              int[] arr = new int[heap.length+1]; 
              for(int x=0;x&lt;heap.length;x++) {
                  arr[x]=heap[x];
              }
              arr[heap.length]=i;
              heap=arr;
              
              
              
          }
          
      }
  
  
      //the sorting in this method starts from the beginning
      private void heapify(int[] heap) {
          int size = heap.length;
          for(int i=0;i&lt;(size/2);i++) {
              int child1=2*i+1;
              if(child1&lt;size) {
                  if(heap[child1] &gt; heap[i]) {
                      int temp = heap[i];
                      heap[i]=heap[child1];
                      heap[child1]=temp;
                  }
              }
              int child2=2*i+2;
              if(child2&lt;size) {
                  if(heap[child2] &gt; heap[i]) {
                      int temp = heap[i];
                      heap[i]=heap[child2];
                      heap[child2]=temp;
                  }
              }
              
          }
          System.out.println(Arrays.toString(heap));
          
      }
      
     //the sorting in this method starts from the end
      private void heapify2(int[] heap) {
          int size = heap.length;
          for(int i=(size/2);i&gt;-1;i--) {
              int child1=2*i+1;
              if(child1&lt;size) {
                  if(heap[child1] &gt; heap[i]) {
                      int temp = heap[i];
                      heap[i]=heap[child1];
                      heap[child1]=temp;
                  }
              }
              int child2=2*i+2;
              if(child2&lt;size) {
                  if(heap[child2] &gt; heap[i]) {
                      int temp = heap[i];
                      heap[i]=heap[child2];
                      heap[child2]=temp;
                  }
              }
              
          }
          System.out.println(Arrays.toString(heap));
          
      }
  
  }
  </code></pre>
  <p>So Here are the questions I have,</p>
  <ol>
  <li><p>How many times do I actually need to call heaping method, in the following method I called heapify method 10 times, equal to size of array, but it sorted the array in about 3-4 iterations,so what's the minimal count of calling heapify method.</p>
  <pre><code>       private void heap() {
           int size = heap.length;
           System.out.println(Arrays.toString(heap));
           //calling heapify method 
            for(int i=0;i&lt;size;i++) {
           heapify(heap);
          }
  
        }
  </code></pre>
  </li>
  <li><p>When I call <code>heapify2()</code> method the output I got was</p>
  <pre><code>       Input: [2, 7, 26, 25, 19, 17, 1, 90, 3, 36]
  
         Output:[90, 36, 26, 7, 25, 17, 1, 2, 3, 19]
  
                              90
                             /   \
                           36     26
                          / \     / \
                         7   25   17  1
                        / \  /
                       2  3  19
  </code></pre>
  </li>
  </ol>
  <p>and when I called <code>heapify()</code> method the output I got was</p>
  <pre><code>          Input: [2, 7, 26, 25, 19, 17, 1, 90, 3, 36]
  
            Output:[90, 36, 17, 25, 26, 7, 1, 2, 3, 19]
  
                                 90
                                /   \
                              36     17
                             / \     / \
                            25  26  7   1
                           / \  /
                          2  3  19
  </code></pre>
  <p>These two methods produce two different binary heaps, I wonder if they are correct and is there a better way to build heap. will deeply appreciate your help on this, thanks in advance.</p>