剑指offer之 调整奇数偶数数组位置

package Problem14;

/*
 * 问题描述:
 * 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数位与数组的前半部分,所有偶数位与数组的
 * 后半部分
 */
public class ReorderOddEven {
    public static void reOrder(int array[]) {
        int firstIndex = 0;
        int lastIndex = array.length - 1;
        if (array == null || (0 == array.length)) {
            return;
        }
        while (firstIndex < lastIndex) {
            while ((firstIndex < lastIndex) && !(isEven(array[firstIndex]))) {
                firstIndex++;
            }
            while ((firstIndex < lastIndex) && isEven(array[lastIndex])) {
                lastIndex--;
            }
            if (firstIndex < lastIndex) {
                int temp = array[firstIndex];
                array[firstIndex] = array[lastIndex];
                array[lastIndex] = temp;
            }
        }
    }

    // 进行解耦操作:odd(奇数)、even(偶数)
    private static boolean isEven(int n) {

        return (n & 1) == 0;
    }

    public static void printArr(int array[]) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + "");
        }
    }

  

原文地址:https://www.cnblogs.com/toov5/p/7656977.html