简体   繁体   English

如何在同一个数组中分离偶数和奇数?

[英]How to seperate even and odd numbers in the same array?

How would I separate even and odd integers in an array with order preserved ?如何在保留顺序的数组中分离偶数和奇数?

Modifications must be in place and the return is a void , and can only use built in methods.修改必须到位并且返回是void ,并且只能使用内置方法。

An example would be:一个例子是:

{4, 5, 8, 16, 45, 12, 67, 13} -> {4, 8, 16, 12, 5, 45, 67, 13}

Explanation解释

You can easily solve this with one iteration, remembering the index of the current border and swapping elements.您可以通过一次迭代轻松解决此问题,记住当前边框的索引并交换元素。

So for your example, the process will be:因此,对于您的示例,该过程将是:

{ 4, 5, 8, 16, 45, 12, 67, 13 } // swap 4 with 4
  ^

{ 4, 5, 8, 16, 45, 12, 67, 13 }
     ^

{ 4, 8, 5, 16, 45, 12, 67, 13 } // swap 5 with 8
        ^

{ 4, 8, 16, 5, 45, 12, 67, 13 } // swap 5 with 16
            ^

{ 4, 8, 16, 12, 45, 5, 67, 13 } // swap 5 with 12
                 ^

Where the ^ shows the current border index, which is always one ahead of the even values, pointing at the index where you want to swap the next even value to.其中^显示当前边界索引,它总是比偶数值早一个,指向您要将下一个偶数值交换到的索引。


First draft第一稿

Here is the code for that:这是代码:

int borderIndex = 0;
for (int i = 0; i < values.length; i++) {
    int value = values[i];
    if (value % 2 == 0) {
        // swap
        values[i] = values[borderIndex];
        values[borderIndex] = value;
        borderIndex++;
    }
}

Preserving order维持秩序

Now, this solution already preserves the order of the even numbers out of the box.现在,这个解决方案已经保留了开箱即用的偶数顺序。 But if you pay close attention you see that it does not preserve order of the odd values.但是,如果您密切注意,您会发现它不会保留奇数值的顺序。 It goes wrong as soon as you have multiple odd values after each other before an even value, like只要在偶数之前有多个奇数值,就会出错,例如

..., 5, 45, 12, ...

because it will then swap 5 with 12 resulting in 12, 45, 5 instead of 12, 5, 45 .因为然后它将512交换,导致12, 45, 5而不是12, 5, 45

Even worse when there are multiple odd values:当有多个奇数值时更糟:

..., 5, 7, 9, 11, 12, ...

resulting in 12, 7, 9, 11, 5 .导致12, 7, 9, 11, 5

In order to fix this, we have to not just swap 5 with the even value 12 but actually swap all the way back to 5 .为了解决这个问题,我们不仅要将5与偶数12交换,而且实际上要一直交换回5 So:所以:

swap 12 with 11
swap 12 with 9
swap 12 with 7
swap 12 with 5

basically shifting down 12 from right to left, until it stands right in front of 5 .基本上从右到左向下移动12 ,直到它位于5的正前方。

We can do so easily with a simple loop that moves from 12 (at i ) to 5 (at borderIndex ):我们可以通过一个从12 (在i处)移动到5 (在borderIndex处)的简单循环轻松做到这一点:

int borderIndex = 0;
for (int i = 0; i < values.length; i++) {
    int value = values[i];
    if (value % 2 == 0) {
        // swap from i to borderIndex
        for (int j = i; j > borderIndex; j--) {
            values[j] = values[j - 1];
            values[j - 1] = value;
        }
        borderIndex++;
    }
}

You can also do it like this.你也可以这样做。 In this case your sorting them on their inherent nature as opposed to their relationship to each other.在这种情况下,您可以根据它们的固有性质对它们进行排序,而不是它们之间的关系。

Integer [] arr = {4, 5, 8, 16, 45, 12, 67, 13};
Arrays.sort(arr, Comparator.comparing(a->a % 2));
System.out.println(Arrays.toString(arr));

prints印刷

[4, 8, 16, 12, 5, 45, 67, 13]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM