title: Heapsort Error during runtime
body: <p>Here is a heapsort program I've created in Java, but I'm having an issue where it won't run.</p>
  
  <p>I'm not getting any errors during compilation, which made the error hard to identify, but if I comment out the size decrement in my extract maximum function the program will run, so I assume that's where the error is. Unfortunately, that line is crucial to the program functioning properly.</p>
  
  <p>If there's anything simple causing this problem, or if major adjustments need to be made to the program, I'd like to know either way.</p>
  
  <p>All input is welcome.</p>
  
  <p><strong>Update</strong>
  added main function.
  Code can now be copy-and-pasted to run.</p>
  
  <p>public class Heap
  {</p>
  
  <pre><code>private int [] data;
  private int [] fin;
  private int size;
  private int tmp = 0;
  
  /**
   * Constructor for objects of class Heap
   */
  public Heap(int[] A)
  {
      data = A;
      size = data.length;
      fin = new int [size];
      this.buildHeap(0);
      for(int n = size - 1; n &gt;= 0; n--)
      {
          fin[n] = this.extractMax();
      }
  }
  
  public int getSize()
  {
      return size;
  }
  
  private void setSize(int i)
  {
      size = i;
  }
  
  public void print()
  {
      for(int i = 0; i &lt; this.getSize(); i++)
      System.out.printf("%d\n", fin[i]);
  }
  
  /**
   * build heap using top down method
   *  
   * @param  i   the index of the node being built upon
   */
  private void buildHeap(int i)
  {
      if(i &lt;= (size - 2)/2)
      {
          buildHeap(2*i + 1);
          buildHeap(2*i + 2);
          heapify(i);
      }
  }
  
  /**
   * Extract maximum number
   * 
   * @return maximum number of heap
   */
  private int extractMax()
  {
      int n = size;
      int store = 0;
      store = data[0];
      data[0] = data[n - 1];
      size--;
      this.heapify(0);
      return store;
  }
  
  /**
   * Heapify array
   * 
   * @param  i  the index to heapify upon
   */
  private void heapify(int i)
  {
      if(2*i + 1 &lt; size &amp;&amp; data[2*i + 1] &gt; data[i])
      {
          if(2*i + 2 &lt; size &amp;&amp; data[2*i + 2] &gt; data[2*i + 1])
          {
              this.exchange(i, 2*i + 2);
              heapify(2*i + 2);
          }
          else
          {
              this.exchange(i, 2*i + 1);
              heapify(2*i + 1);
          }
      }
      if(2*i + 2 &lt; size &amp;&amp; data[2*i + 2] &gt; data[i])
      {
          this.exchange(i, 2*i + 2);
          heapify(2*i + 2);
      }
  }
  
  private boolean exchange(int i, int k)
  {
      tmp = data[i];
      data[i] = data[k];
      data[k] = tmp;
      return true;
  }
  
  public static void main(String [] args)
  {
      int [] arr = {5,13,2,25,7,17,20,8,4};
      Heap heapsort = new Heap(arr);
      heapsort.print();
  }
  </code></pre>
  
  <p>}</p>