/*
输入一个整数数组,实现一个函数来调整该数组中数字的顺序,
使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,
并保证奇数和奇数,偶数和偶数之间的相对位置不变
*/
import java.util.*;
public class Class17 {
public void shuzipaixu(int[] array){
if(array == null || array.length == 0){
return;
}
int odd = 0;
int even = array.length - 1;
int[] newArray = new int[array.length];
int countOdd = 0;
int countEven = array.length - 1;
for(int i = 0; i < array.length; i++){
if(array[i] % 2 != 0){
newArray[countOdd] = array[i];
countOdd++;
}
}
for(int j = array.length - 1; j >= 0; j--){
if(array[j] % 2 == 0){
newArray[countEven] = array[j];
countEven--;
}
}
for(int t = 0; t < array.length; t++){
array[t] = newArray[t];
}
}
public void test() {
int[] array = {4,7,8,9,3,6,5,1};
System.out.println("原始数组:"+Arrays.toString(array));
shuzipaixu(array);
System.out.println("调整结果:"+Arrays.toString(array));
System.out.println();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Class17 c = new Class17();
c.test();
}
}