给定一数组,要求奇数在前,偶数在后排序(不要求大小顺序)
public static void main(String[] args) {
int a[]= {1,4,6,8,9,5,7,5,2,5,9,4,0,1,5,8,3,4};
int next1=0;
int next2=a.length-1;
while(next1!=next2) {
if(a[next1]%2==0&&a[next2]%2==1) {
jh(a,next1,next2);
}
if(a[next1]%2!=0) {
next1++;
}
if(a[next2]%2!=1) {
next2--;
}
for(int i:a) {
System.out.print(i+" ");
}
System.out.println( );
}
}
private static void jh(int[] a, int next1, int next2) {
int t=a[next1];
a[next1]=a[next2];
a[next2]=t;
}