#include <iostream>
using namespace std;
//两个指针夹逼
void OddBeforeEven(int* array,int len){
if(array==NULL||len<=0) return;
int *begin=array;
int *end=array+len-1;
while(begin<end){
while(begin<end&&(*begin&0x1)!=0)
begin++;
while(begin<end&&(*end&0x1)==0)
end--;
if(begin<end){
int tmp=*begin;
*begin=*end;
*end=tmp;
}
}
}
//将A置于B之前的通用解法,函数参数
void ABeforeB(int *array,int len,bool (*func)(int)){
if(array==NULL||len<=0) return;
int *begin=array;
int *end=array+len-1;
while(begin<end){
while(begin<end&&!func(*begin))
begin++;
while(begin<end&&func(*end))
end--;
if(begin<end){
int tmp=*begin;
*begin=*end;
*end=tmp;
}
}
}
bool isEven(int n){
if((n&0x1)==0) return true;
else return false;
}
void OddBeforeEven_2(int* array,int len){
ABeforeB(array,len,isEven);
}
int main(){
int array[5]={1,2,3,4,5};
int array_2[5]={1,2,3,4,5};
OddBeforeEven(array,5);
OddBeforeEven_2(array_2,5);
for(int i=0;i<5;++i){
cout<<array[i];
}
for(int i=0;i<5;++i){
cout<<array_2[i];
}
}