title: List not sorted after applying HeapSort implemented in Java
body: <p>I am trying to implement heapSort in java but not getting the sorted list after executing. Can you suggest what i am doing wrong here? I created a method Heapsort to sort the heap which calls heap itself. Once the heap is called it gives me a heap tree containing the largest element at the top. I am assuming my interchange or further process is not working properly. What's wrong in here?</p>
  
  <pre><code>  public class HeapSort 
    {
     public static void main(String args[])
     {
      int a[]=new int[]{30,100,20,80,40,90,50,60};
      for(int i=0;i&lt;a.length;i++)
      {
          System.out.print(a[i]+"\t");
      }
      System.out.println("");
      heapSort(a);
      for(int i=0;i&lt;a.length;i++)
      {
          System.out.print(a[i]+"\t");
      }
  }
  public static void heapSort(int a[])
  {
      int i, temp;
      for(i=a.length-1;i&gt;=0;i--)
      {
          heap(a);
          temp=a[0];
          a[0]=a[i];
          a[i]=temp;
      }
  }
  public static void heap(int a[])
  {
      int i,j,k,temp;
      for(i=0;i&lt;a.length;i++)
      {
          j=i;
          k=(i-1)/2;
          while(k&gt;=0&amp;&amp;a[j]&gt;a[k])
          {
              temp=a[j];
              a[j]=a[k];
              a[k]=temp;
              j=j/2;
              k=k/2;
          }
      }
  }
  }
  </code></pre>