题目
设计将所有奇数移到所有偶数之前的算法
算法思想:利用快速排序的思想
代码
#include <iostream>
using namespace std;
//算法主体
void move(int a[],int n){
int i=0,j=n-1;
int temp;
while(i<j){
while(a[j]%2==0&&i<j) //从数组后面找第一个奇数
j--;
while(a[i]%2==1&&i<j) //从数组前面找到第一个偶数
i++;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
int main() {
int a[10]={2, 1, 5, 2, 1, 7, 9, 5, 3, 8};
int n=10;
cout<<"排序前:"<<endl;
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
move(a,n);
cout<<"排序后:"<<endl;
for(int j=0;j<n;j++)
cout<<a[j]<<" ";
cout<<endl;
return 0;
}