package com.sort;
//输入一个整数数组,调整数组中数字的顺序,使得所有奇数位于偶数前面
public class Test1 {
public static void main(String args[]) {
int a[] = { 1, 2, 3, 4, 5, 6, 7, 9, 10, 12, 14, 11 };
int n = 12;
System.out.println("before transformation\n");
for (int i = 0; i < n; i++)
System.out.print(a[i]+" ");
System.out.println();
transformation(a, n);
return;
}
public static boolean isEven(int a) {
return a % 2 == 0 ? true : false;
}
public static void transformation(int data[], int n) {
int begin = 0;
int end = n - 1;
while (begin < end) {
if (!isEven(data[begin])) {
begin++;
continue;
}
if (isEven(data[end])) {
end--;
continue;
}
int c = data[begin];
data[begin] = data[end];
data[end] = c;
}
System.out.println("after transformation\n");
for (int i = 0; i < n; i++)
System.out.print(data[i]+" ");
System.out.println();
}
}
package com.sort;
//输入一个整数数组,调整数组中数字的顺序,使得所有奇数位于偶数前面
public class Test {
public static void main(String args[]) {
int[] a = { 2, 3, 4, 2, 3, 1, 6, 1 };
int c = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] % 2 > 0) {
continue;
} else {
for (int j = i + 1; j < a.length; j++) {
if (a[j] % 2 > 0) {
c = a[i];
a[i] = a[j];
a[j] = c;
} else {
continue;
}
}
}
}
for(int j = 0 ; j < a.length;j++){
System.out.print(a[j]+" ");
}
}
}