title: creating a max-heap using java
body: <p>I am trying to create a Max-Heap in java using the following code:</p>
  
  <pre><code>public class Heapify {
  // 16 14 10 8 7 9 3 2 4 1
  
      public static int[] Arr = {4, 1, 3, 2, 16, 9, 10, 14, 8, 7};
      public static int counter = 0;
  
      public static void main(String[] args) {
          int kk;
          for (kk = 0; kk &lt;= Arr.length - 1; kk++) {
              heapM(Arr, kk);
          }
  
          for (int krk = 0; krk &lt; Arr.length; krk++) {
              System.out.println(Arr[krk]);
          }
  
  
  
      }
  
      public static void heapM(int[] Arr, int i) {
  
          int largest;
          int left = i * 2;
          int right = i * 2 + 1;
          if (((left &lt; Arr.length) &amp;&amp; (Arr[left] &gt; Arr[i]))) {
              largest = left;
          } else {
              largest = i;
          }
  
          if (((right &lt; Arr.length) &amp;&amp; (Arr[right] &gt; Arr[largest]))) {
              largest = right;
          }
          if (largest != i) {
              swap(i, largest);
  
  
              heapM(Arr, largest);
          }
      }
  
      private static void swap(int i, int largest) {
          int t = Arr[i];
          Arr[i] = Arr[largest];
          Arr[largest] = t;
  
      }
  }
  </code></pre>
  
  <p>The desired output should be :</p>
  
  <pre><code>16 14 10 8 7 9 3 2 4 1
  </code></pre>
  
  <p>Whereas I am getting </p>
  
  <pre><code>4 3 16 14 8 9 10 2 1 7
  </code></pre>
  
  <p>Can someone please help as to why the heap is not being built properly ? </p>
  
  <p>Thanks</p>