title: Algorithm to bring evens to front of array: How to incorporate corner case elegantly?
body: <p>I actually have a few problems pertaining to my attempt at an implementation of the "Bring the evens to the front" or similar partition-type interview question. Here's the implementation:</p>
  
  <p><a href="http://codepad.org/eO0c6Sbj" rel="nofollow">http://codepad.org/eO0c6Sbj</a></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  void intSwap ( int * a, int * b )
  {
      int temp = *a;
      *a = *b; 
      *b = temp;
  }
  
  void evensToFront ( int * arr, int n )
  {
      if (n &lt; 2) return; // if size of array is 0 or 1
      int * end = arr + n - 1; // pointer to last element
      do
      {
         if (*arr % 2 == 1) // if first pointer is odd
         {
             if (*end % 2 == 0) // if second pointer is even
             {
                 intSwap(arr,end); // swap first and second
             }
             else // second pointer is not even
             {
                 // move second pointer towards first until even or hits first
                 do --end;
                 while ((arr != end) &amp;&amp; (*arr % 2 == 1));
             }
         }   
         else
         {
             ++arr;
         }  
      } while (arr != end); // while first pointer not equal to second
  
  }
  
  
  int main ( )
  {
      int arr [] = {0, 69, -13, 100, 2, 2, 5};
      int len = sizeof(arr)/sizeof(int);
      evensToFront(arr,len);
      int * p1 = arr, * p2 = (arr + len);
      for (;p1!=p2;++p1) printf("%d ", *p1);
      return 0;
  }
  </code></pre>
  
  <ol>
  <li>Why is the output <code>0 69 -13 100 2 2 5</code>? In other words, where is the flaw in my algorithm?</li>
  <li>Why is the online compiler not letting me define variables within <code>for</code> loops? When I change the <code>for</code> loop to the more straightforward <code>for (int k = 0; k &lt; len; ++k) printf("%d ", arr[k]);</code> I get the error </li>
  </ol>
  
  <blockquote>
    <p>'for' loop initial declaration used outside C99 mode.</p>
  </blockquote>
  
  <ol start="3">
  <li>How do I get rid of the line <code>if (n &lt; 2) return;</code> that I have for dealing with the corner case? Ideally, the corner case would be dealth with in the logic of the outermost <code>while</code> loop without adding any extra operations to the algorithm.</li>
  </ol>