title: Why does my heap sort not work when I use the array of more than 10? c#
body: <p>Code:</p>
  
  <pre><code>class Program
  {
      static void HeapSort(int[] array, int n) 
      {
          for (int i = n / 2 - 1; i &gt;= 0; i--) 
              Heapify(array, n, i);
          for (int i = n - 1; i &gt;= 0; i--)       
          {
              int temp = array[0];
              array[0] = array[i];
              array[i] = temp;
              Heapify(array, i, 0);
          }
      }
      static void Heapify(int[] array, int n, int i)
      {
          int largest = i;                                   // largest = 4
          int left = 2 * i + 1;                              // left = 9 
          int right = 2 * i + 2;                             // right = 10
          if (left &lt; n &amp;&amp; array[left] &gt; array[largest])      //ar left &lt; 10 ir ar ar9 &gt; ar4
              largest = left;                                //left tampa 
          if (right &lt; n &amp;&amp; array[right] &gt; array[largest])
              largest = right;
          if (largest != i)
          {
              int swap = array[i];
              array[i] = array[largest];
              array[largest] = swap;
              Heapify(array, n, largest);
          }
      }
      public static void Main()
      {
          int[] arr = { 55, 25, 89, 34, 12, 19, 78, 95, 1, 100, 99, 98};
          int n = 12, i;
          Console.WriteLine("Heap Sort");
          Console.Write("Initial array is: ");
          for (i = 0; i &lt; n; i++)
          {
              Console.Write(arr[i] + " ");
          }
          HeapSort(arr, 10);
          Console.Write("\nSorted Array is: ");
          for (i = 0; i &lt; n; i++)
          {
              Console.Write(arr[i] + " ");
          }
          Console.ReadLine();
      }
  }
  </code></pre>
  
  <p>Works fine when I use the array of 10, but if I add elements, they are just put at the end of the array. Example:</p>
  
  <pre><code>int[] arr = { 55, 25, 89, 34, 12, 19, 78, 95, 1, 100};
          int n = 10, i;
  </code></pre>
  
  <p>Output:
  Heap Sort
  Initial array is: 55 25 89 34 12 19 78 95 1 100
  Sorted Array is: 1 12 19 25 34 55 78 89 95 100</p>
  
  <p>now, if I add a couple more:</p>
  
  <pre><code>int[] arr = { 55, 25, 89, 34, 12, 19, 78, 95, 1, 100, 99, 98, 120, 44};
          int n = 14, i;
  </code></pre>
  
  <p>Output:
  Heap Sort
  Initial array is: 55 25 89 34 12 19 78 95 1 100 99 98 120 44
  Sorted Array is: 1 12 19 25 34 55 78 89 95 100 99 98 120 44</p>
  
  <p>Why doesnt it sort the rest?</p>