title: how to count heapsort key comparisons
body: <p>I wrote a little C# command application. Four arrays are supposed to be sorted with a heapsort algorithm. I took an algorithm from a website and its running just fine. Now I want to count the key-comparisons the algorithm needs to sort one array. I tried to count the comparisons via for loop but its seems to be wrong... Any ideas where I have to count for it?</p>
  
  <p>Here's my sorting algorithm method. <code>GlobalVar.CountVal</code> is simply a <code>public static int</code> property.</p>
  
  <pre><code>public static void HeapSort(int[] array, int arr_ubound)
  {
      int i, j;
      int lChild, rChild, pNode, root, temp;
  
      root = (arr_ubound - 1) / 2;
  
      for (j = root; j &gt;= 0; j--)
      {
          for (i = root; i &gt;= 0; i--)
          {
              GlobalVar.CountVal += 1;
              lChild = (2*i)+1;
              rChild = (2*i)+2;
  
              if ((lChild &lt;= arr_ubound) &amp;&amp; (rChild &lt;= arr_ubound))
              {
                  if (array[rChild] &gt;= array[lChild])
                      pNode = rChild;
                  else
                      pNode = lChild;
              }
              else
              {
                  if (rChild &gt; arr_ubound)
                      pNode = lChild;
                  else
                      pNode = rChild;
              }
  
              if (array[i] &lt; array[pNode])
              {
                  temp = array[i];
                  array[i] = array[pNode];
                  array[pNode] = temp;
              }
          }
      }
  
      temp = array[0];
      array[0] = array[arr_ubound];
      array[arr_ubound] = temp;
      return;
  }
  </code></pre>
  
  <p>Here's the full code: <a href="http://pastebin.com/4Y0NQECP" rel="nofollow">http://pastebin.com/4Y0NQECP</a></p>