[英]Sorting even and odd numbers inside an array
I'm trying to split the array into odd
and even
numbers. 我正在尝试将数组拆分为odd
和even
。 Note that sorting numbers in the final result does not matter. 请注意,在最终结果中排序数字无关紧要。 I'm compiling the code and the output contains some bug. 我正在编译代码,输出包含一些bug。 My code arranges odd
numbers correctly while the even
numbers are giving me some trouble. 我的代码正确排列odd
,而even
给我一些麻烦。 Could somebody please help me out with the arrangement of even
numbers? 有人可以帮我解决even
吗?
Basically, I arrange odd
numbers in the left side of the array and have oddPos = 0
in the beginning; 基本上,我在数组的左侧排列odd
,并在开头有oddPos = 0
; even
numbers are in the right side and the positioning starts from the very end of the array evenPos = myArray.length - 1
. even
位于右侧,定位从数组的最后开始evenPos = myArray.length - 1
。
public class EvenOddArray {
public static void main(String[] args){
int[] myArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int oddPos = 0;
int evenPos = myArray.length - 1;
for(int i = 0; i < myArray.length; i++){
if(myArray[i] % 2 == 0){
myArray[evenPos] = myArray[i];
evenPos--;
}
else{
myArray[oddPos] = myArray[i];
oddPos++;
}
}
for(int i = 0; i < myArray.length; i++){
System.out.print(myArray[i] + " ");
}
}
}
Output: 输出:
1 3 5 7 2 4 6 6 4 2
int current = 0;
int evenPos = myArray.Length - 1;
while (current < evenPos) {
if (myArray[current] % 2 == 0) {
swap(myArray, evenPos, current);
evenPos--;
} else {
current++;
}
}
A squeezed funny version: 一个挤压有趣的版本:
for (int curPos=0, evenPos=myArray.length-1; curPos < evenPos;)
if (myArray[curPos] % 2 == 0)
swap(myArray, evenPos--, curPos);
else
curPos++;
More fun version: 更有趣的版本:
for (int curPos=0, evenPos=myArray.length-1; curPos < evenPos;)
swap(myArray, curPos, myArray[curPos]%2==0 ? evenPos-- : curPos++);
explanation: 说明:
You don't have to swap values when the number is odd. 当数字为奇数时,您不必交换值。 you only increase the current counter. 你只增加当前的计数器。
you can't use the for loop counter as an index to the array too. 你也不能使用for循环计数器作为数组的索引。 to not miss the numbers that gets swapped to the counter index not processed. 不要错过交换到未处理的计数器索引的数字。 this is the mistake that other answers didn't cover. 这是其他答案没有涵盖的错误。
Actually you are editing the same myArray
array while reading from it. 实际上你正在编辑同一个myArray
数组,同时从中读取它。 So what happens is, 那么会发生什么,
You insert 6 into the myArray[7]
th position, in the 6th iteration of the loop. 在循环的第6次迭代中,将6插入myArray[7]
位置。 So, during the 7th iteration when you read the myArray[7]
, it is 6. Not 8. Because, you have over written 8 with 6 in the previous iteration. 因此,在第7次迭代期间,当您读取myArray[7]
,它是6.不是8.因为,在上一次迭代中,您已经用8写了6。
Therefore, use a separate array to hold the results. 因此,使用单独的数组来保存结果。 Hope you get the point. 希望你明白这一点。
You can do something like this, 你可以这样做,
int[] myArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] resultArray = new int[myArray.length];
int oddPos = 0;
int evenPos = myArray.length - 1;
for(int i = 0; i < myArray.length; i++){
if(myArray[i] % 2 == 0){
resultArray[evenPos] = myArray[i];
evenPos--;
}
else{
resultArray[oddPos] = myArray[i];
oddPos++;
}
}
Lets see what happens with each Iteration of your for loop. 让我们看看for循环的每次迭代会发生什么。
As you can see, you are modifying the array "inplace". 如您所见,您正在修改“inplace”数组。 You are modifying the array without using all the values. 您正在修改数组而不使用所有值。 For example, look at 9, It gets over written before it is ever accessed. 例如,看看9,它在被访问之前就被覆盖了。 So, your algo is wrong. 所以,你的算法是错的。
Suggestions: 建议:
Here is my optimized version, which uses around half of the swaps compared to the @hasan83 version. 这是我的优化版本,与@ hasan83版本相比,它使用大约一半的掉期。
int n = myArray.length;
int oddPos = 0;
int evenPos = n - 1;
while (true) {
while (oddPos < n && myArray[oddPos] % 2 == 1) {
oddPos++;
}
while (evenPos >= 0 && myArray[evenPos] % 2 == 0) {
evenPos--;
}
if (oddPos >= evenPos) break;
swap(myArray, oddPos, evenPos);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.