title: Having trouble with Downheap Algorithm via array implementation
body: <p>I am currently trying to sort my array of keys using the Down-heap algorithm. However, when I display my newly sorted array, new numbers appear and the order doesn't seem right. I cannot tell if something is wrong with my algorithm or if I'm not using the proper conditions for this sorting algorithm.</p>
  
  <p>My job is to sort an array of 20 keys.</p>
  
  <p><strong>The Code:</strong></p>
  
  <pre><code>/* Downheap sorting algorithm */
  for(i=0; i&lt;20; i++)
  {
      j = i + 1;
  
      /* If parent is less than both children */
      if(key[j] &lt; key[2*j] &amp;&amp; key[j] &lt; key[(2*j)+1])
      {
          /* Check which child is smallest */
          if(key[2*j] &lt; key[(2*j)+1])
          {
              /* Parent is assigned smallest node */
              swap(&amp;key[j],&amp;key[2*j]);
          }
          else{swap(&amp;key[j],&amp;key[(2*j)+1]);}
      }
  
      /* If parent is less than left child */
      else if(key[j] &lt; key[2*j])
      {
          swap(&amp;key[j],&amp;key[2*j]);
      }
  
      /* If parent is less than right child */
      else if(key[j] &lt; key[(2*j)+1])
      {
          swap(&amp;key[j],&amp;key[(2*j)+1]);
      }
  }
  </code></pre>
  
  <p><strong>The Swap Function:</strong></p>
  
  <pre><code>void swap(int *parent, int *child)
  {
      int temp;
      temp = *child;
      *child = *parent;
      *parent = temp; 
  }
  </code></pre>
  
  <p>Array of keys <strong>before</strong> sorting:</p>
  
  <pre><code>54,90,137,260,185,65,208,139,114,176,186,77,137,139,178,57,203,110,80,127
  </code></pre>
  
  <p>Array of keys <strong>after</strong> sorting once:</p>
  
  <pre><code>54,137,185,260,114,77,208,178,110,176,186,65,137,139,139,64,203,90,84,127
  </code></pre>
  
  <p>64 was not there before. Where is 57? 80 has disappeared and where did 84 come from?</p>
  
  <p>Any help would be much appreciated!</p>