思路:类似快速排序的处理,从左边扫描直到找到偶数,再从右边扫描直至找到奇数,再交换。
int[] a = {1,2,3,4,5,6,7,8,9,10};
public void oddAndEven() {
int i = 0;
int j = a.Length-1;
int temp;
while(true) {
while(i<a.Length && (a[i]%2 == 0))
{
i++;
}
while(j>= 0 && (a[j]%2 == 1))
{
j--;
}
if( i > j ) break;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
for(i=0; i<a.Length; i++) {
Console.WriteLine(a[i] + " ");
} |