//线性存储的顺序表,把奇数都放到偶数前面
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#define MAX 20
void swap(int& a, int& b) {
int temp;
temp = a;
a = b;
b = temp;
}
void change(int A[],int n) {
int low = 0, high = n - 1;
while (low<high)
{
while (low < high && A[low] % 2 == 1) low++; //寻找偶数
while (low < high && A[high] % 2 == 0) high--; //寻找奇数
if (low < high) {
swap(A[low], A[high]);//交换+移动
low++;
high--;
}
}
}
int main() {
int A[] = { 3,6,4,8,2,1,7 };
int n = 7;
change(A, n);
for (int j = 0; j < 7; j++) {
printf("%d", A[j]);
}
return 0;
}
12-09
164
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-30
1725
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)