简体   繁体   中英

Sorting even and odd numbers inside an array

I'm trying to split the array into odd and even numbers. Note that sorting numbers in the final result does not matter. I'm compiling the code and the output contains some bug. My code arranges odd numbers correctly while the even numbers are giving me some trouble. Could somebody please help me out with the arrangement of even numbers?

Basically, I arrange odd numbers in the left side of the array and have oddPos = 0 in the beginning; even numbers are in the right side and the positioning starts from the very end of the array 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. 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. So what happens is,

You insert 6 into the myArray[7] th position, in the 6th iteration of the loop. 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.

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.

  • Original: 1 2 3 4 5 6 7 8 9 10
  • 1st Iter: 1 2 3 4 5 6 7 8 9 10
  • 2nd Iter: 1 2 3 4 5 6 7 8 9 2
  • 3rd Iter: 1 3 3 4 5 6 7 8 9 2
  • 4th Iter: 1 3 3 4 5 6 7 8 4 2
  • 5th Iter: 1 3 5 4 5 6 7 8 4 2
  • 6th Iter: 1 3 5 4 5 6 7 6 4 2
  • 7th Iter: 1 3 5 7 5 6 7 6 4 2
  • 8th Iter: 1 3 5 7 5 6 6 6 4 2
  • 9th Iter: 1 3 5 7 5 4 6 6 4 2
  • 10th Iter: 1 3 5 7 2 4 6 6 4 2

As you can see, you are modifying the array "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. So, your algo is wrong.

Suggestions:

  • Use a new array to hold the results as in tibzon's answer
  • Use swapping instead of overwriting. You have to update your algo accordingly. I was going to provide one. But Murenik already provided one.

Here is my optimized version, which uses around half of the swaps compared to the @hasan83 version.

    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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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