首页 > 代码库 > 数组奇在偶前
数组奇在偶前
public class shuzujizaihouzhiqian {
//private static final String[][] String = null;
/**
* @param args
* @return
*/
public static void sort(int[] x){
if(x==null||x.length==0){
return ;
}
int len=x.length;
int[] tmp=new int[len];
int oddPos=0;
int evenPos=len-1;
for(int i=0;i<len;i++){
if(!isEven(x[i])){
tmp[oddPos++]=x[i];
}else{
tmp[evenPos--]=x[i];
}
}
System.arraycopy(tmp, 0, x, 0, len);
}
public static boolean isEven(int x){
return (x&1)==0;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] tem={1,2,5,4,9,34,97,35,68};
sort(tem);
String[] str=new String[tem.length];
for (int i = 0; i < tem.length; i++) {
str[i]=tem[i]+"";
System.out.print(str[i]+" ");
}
}
}
数组奇在偶前