#include <stdio.h>
int oddNum(int num)
{
if (num % 2 == 1)
return 1;
else
return 0;
}
void swap(int A[], int i, int j)
{
int t = A[i]; A[i] = A[j]; A[j] = t;
}
void printArray(int A[], int size)
{
int i;
for (i = 0; i < size; ++i)
printf("%d ", A[i]);
putchar('\n');
}
void partition(int A[], int beg, int end)
{
while (beg < end)
{
if ( oddNum(A[beg]) )
++beg;
else if ( (oddNum(A[beg]) == 0) && (oddNum(A[end]) == 0) )
--end;
else
swap(A, beg++, end--);
}
}
int main()
{
int A[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int size = sizeof(A) / sizeof(int);
int i = 0, j = size - 1;
partition (A, i, j);
printArray(A, size);
return 0;
}
oddNumFrist
最新推荐文章于 2014-12-16 16:30:10 发布