Prove safety offer 13. adjusted so that the array sequentially in front of even-odd

13. The order of the array so that the adjustment in front of even-odd

topic

Enter an array of integers, to realize a function to adjust the order of the numbers in the array, such that all the odd part of the front half of the array, is located in the second half of all the even array, and between odd and ensure a relatively even, odd and even the same position.

Thinking

Using space for time, to ensure that the relative position, a new array of odd-numbered even-numbered put back in front of the discharge, and then assigned to the original array.

Code

    public void reOrderArray(int[] array) {
    int[] newarray = new int[array.length];
    int index = 0;
    for (int i = 0; i < array.length; i++) {
      if (array[i] % 2 == 1) {
        newarray[index] = array[i];
        index++;
      }
    }
    for (int i = 0; i < array.length; i++) {
      if (array[i] % 2 == 0) {
        newarray[index] = array[i];
        index++;
      }
    }
    for (int i = 0; i < array.length; i++) {
      array[i] = newarray[i];
    }
  }

Guess you like

Origin www.cnblogs.com/blogxjc/p/12376146.html