title: Interview question: C program to sort a binary array in O(n)
body: <p>I've come up with the following program to do it, but it does not seem to work and goes into infinite loop. Its working is similar to quicksort.</p>
  
  <pre><code>int main()
  {
   int arr[] = {1,1,0,1,0,0,0,1,0,1,0,1,0,1,0,1,0,1};
   int N = 18;
   int *front, *last;
  
   front = arr;
   last = arr + N;
   while(front &lt;= last)
   {
    while( (front &lt; last) &amp;&amp; (*front == 0) )
     front++;
  
    while( (front &lt; last) &amp;&amp; (*last == 1) )
     last--;
  
    if( front &lt; last)
    {
     int temp = *front;
     *front = *last;
     *last = temp;
     front ++;
     last--;
    }
   }
   for(int i=0;i&lt;N;i++)
    printf("%d ",arr[i]);
  
   return 0;
  }
  </code></pre>