public class Reorder {
public static void main(String[] args) {
int[] list = { 1, 2, 3, 4, 5, 7, 8, 9, 11, 12 };
reorderOddEven(list);
}
public static void reorderOddEven(int[] list) {
int length = list.length;
for (int i = 0; i < length; i++) {
System.out.print(list[i] + " ");
}
System.out.print("\n");
int begin = 0;
int end = length - 1;
//可以理解为数组头尾各有一个游标,位于开头的游标遇到偶数停止,位于结尾的游标遇到奇数停止
while (begin < end) {
//为奇数判断,begin自加,遇到偶数则停止
while (begin < end && (list[begin] & 0x1) != 0)
// while (begin < end && (list[begin] % 2) != 0)
// System.out.println("begin::" + list[begin] + "能否除尽:"+ list[begin] % 2);
begin++;
while (begin < end && (list[end] & 0x1) == 0)
end--;
if (begin < end) {
int temp = list[begin];
list[begin] = list[end];
list[end] = temp;
}
}
for (int i = 0; i < length; i++) {
System.out.print(list[i] + " ");
}
}
}
转载于:https://my.oschina.net/yZLZkvk6bjiN/blog/491999