title: Heap Corruption detected: after normal block (#138) at 0x00520568
body: <pre><code>#include&lt;iostream&gt;
  #include &lt;stdio.h&gt;   
  #include &lt;stdlib.h&gt;     
  #include &lt;time.h&gt;
  
  using namespace std;
  
  void max_heapify(int *a, int i, int n)
  {
      int j, temp;
      temp = a[i];
      j = 2*i;
      while (j &lt;= n)
      {
          if (j &lt; n &amp;&amp; a[j+1] &gt; a[j])
          {
              j = j+1;
          }
          if (temp &gt; a[j])
          {
              break;
          }
          else if (temp &lt;= a[j])
          {
              a[j/2] = a[j];
              j = 2*j;
          }
      }
      a[j/2] = temp;
      return;
  }
  void heapsort(int *a, int n)
  {
      int i, temp;
      for (i = n; i &gt;= 2; i--)
      {
          temp = a[i];
          a[i] = a[1];
          a[1] = temp;
          max_heapify(a, 1, i - 1);
      }
  }
  void build_maxheap(int *a, int n)
  {
      int i;
      for(i = n/2; i &gt;= 1; i--)
      {
          max_heapify(a, i, n);
      }
  }
  
  int main()
  {
      clock_t t,t2;
      int * a = new int[10]; 
      int any = 9;
      for(int i = 0; i &lt; 10; i++)
      {
          a[i] = rand() % 9 + 1;  
      }
      t = clock();
      build_maxheap(a,10);
      heapsort(a, 10);
      t2 = clock();
  
      cout&lt;&lt;endl&lt;&lt;endl&lt;&lt;t2-t&lt;&lt;endl&lt;&lt;endl;
  
      delete [] a;
  
      system("pause");
  
  }
  </code></pre>
  
  <p>I am trying to run heapsort on an array randomly sorted to check how much time it takes. but it give me the error of heap corruption. I took the algo from internet. I ran the same array on merge and insertion sort and it worked fine.</p>