类似于Dutch Flag问题
void segregateEvenOdd(int arr[], int size)
{
/* Initialize left and right indexes */
int left = 0, right = size-1;
while(left < right)
{
/* Increment left index while we see 0 at left */
while(arr[left]%2 == 0 && left < right) //%可以优化--->arr[left]&1 == 0
left++;
/* Decrement right index while we see 1 at right */
while(arr[right]%2 == 1 && left < right)
right--;
if(left < right)
{
/* Swap arr[left] and arr[right]*/
swap(&arr[left], &arr[right]);
left++;
right--;
}
}
}
另有 Segregate 0s and 1s in an arrayhttp://www.geeksforgeeks.org/segregate-0s-and-1s-in-an-array-by-traversing-array-once/