title: Why aren't the middle elements getting sorted?
body: <p>I'm implementing a sorting algorithm for fun, but for reasons I don't know, what I've implemented so far doesn't sort the middle. Here are the steps of the algorithm:</p>
  <ol>
  <li>Get the indices of the minimum and maximum values</li>
  <li>Move the minimum value to the minimum index, the maximum value to the maximum index</li>
  <li>Increment the minimum index and decrement the maximum index</li>
  <li>Repeat from 1. while the minimum index is less than the maximum index</li>
  </ol>
  <p>I know this isn't the most efficient sorting algorithm, but it's just for fun. Here is the code</p>
  <pre><code>#define ui64 uint64_t
  
  void print(ui64* array, ui64 ArraySize);
  
  ui64 random(void) 
  {
    ui64 r = 0;
    for(int i = 0; i &lt; 64; i++) r = r*2 + rand()%2;
    return r;
  }
  
  void generate(ui64* array, ui64 ArraySize);
  
  Pair getMinMaxIndices(ui64* array, ui64 minIndex, ui64 maxIndex)
  {
      Pair minmax, minmaxIndices;
      
      if(maxIndex == 0)
      {
          minmax.max = array[0];
          minmax.min = array[0];
          
          minmaxIndices.max = 0;
          minmaxIndices.min = 0;
          
          return minmaxIndices;
      }
      
      if(array[minIndex] &gt; array[minIndex + 1])
      {
          minmax.max = array[minIndex];
          minmax.min = array[minIndex + 1];
          
          minmaxIndices.max = minIndex;
          minmaxIndices.min = minIndex + 1;
      }
      else
      {
          minmax.max = array[minIndex + 1];
          minmax.min = array[minIndex];
          
          minmaxIndices.max = minIndex + 1;
          minmaxIndices.min = minIndex;
      }
  
      for(int i = minIndex + 2; i &lt; maxIndex + 1; i++)
      {
          if(array[i] &gt; minmax.max)
          {
              minmax.max = array[i];
              minmaxIndices.max = i;
          }
  
          else if(array[i] &lt; minmax.min)
          {
              minmax.min = array[i];
              minmaxIndices.min = i;
          }
      }
  
      return minmaxIndices;
  }
  
  void swap(ui64* array, ui64 index1, ui64 index2);
  
  void closingCurtainsSort(ui64* array, ui64 ArraySize)
  {
      Pair minmaxIndices, indexLimits;
      
      indexLimits.min = 0;
      indexLimits.max = ArraySize - 1;
      
      while(indexLimits.min &lt; indexLimits.max)
      {
          minmaxIndices = getMinMaxIndices(array, indexLimits.min, indexLimits.max);
          
          swap(array, minmaxIndices.min, indexLimits.min);
          swap(array, minmaxIndices.max, indexLimits.max);
          
          indexLimits.min++;
          indexLimits.max--;
      }
  }
  </code></pre>
  <p>Here is the step by step as generated by the program for this hardcoded array: array2[10] = {0,2,4,6,8,1,3,5,7,9}</p>
  <pre><code>Array2
  0
  2
  4
  6
  8
  1
  3
  5
  7
  9
  Current minimum value -&gt; array[0]: 0
  Current maximum value -&gt; array[9]: 9
  Current minimum index -&gt; 0
  Current maximum index -&gt; 9
  
  Loop 1
  0
  2
  4
  6
  8
  1
  3
  5
  7
  9
  Current minimum value -&gt; array[0]: 0
  Current maximum value -&gt; array[9]: 9
  Current minimum index -&gt; 1
  Current maximum index -&gt; 8
  
  Loop 2
  0
  1
  4
  6
  7
  2
  3
  5
  8
  9
  Current minimum value -&gt; array[5]: 2
  Current maximum value -&gt; array[4]: 7
  Current minimum index -&gt; 2
  Current maximum index -&gt; 7
  
  Loop 3
  0
  1
  2
  6
  5
  4
  3
  7
  8
  9
  Current minimum value -&gt; array[5]: 4
  Current maximum value -&gt; array[4]: 5
  Current minimum index -&gt; 3
  Current maximum index -&gt; 6
  
  Loop 4
  0
  1
  2
  6
  5
  4
  3
  7
  8
  9
  Current minimum value -&gt; array[6]: 3
  Current maximum value -&gt; array[3]: 6
  Current minimum index -&gt; 4
  Current maximum index -&gt; 5
  
  Loop 5
  0
  1
  2
  6
  5
  4
  3
  7
  8
  9
  Current minimum value -&gt; array[5]: 4
  Current maximum value -&gt; array[4]: 5
  Current minimum index -&gt; 5
  Current maximum index -&gt; 4
  </code></pre>
  <p>EDIT:
  Removed the following:</p>
  <ul>
  <li>included headers</li>
  <li>documentation</li>
  <li>debugging function</li>
  <li>main()</li>
  </ul>
  <p>Declared functions with obvious implementation instead of defining</p>