1

My task is to write a function that rearranges an array so that the odd numbers occur in the beginning of the array, from greatest to least, and the even numbers from least to greatest at the end. We are not allowed to use any other libraries except for the standard input and output streams.

The output works when the numbers are:

{-15, 450, 6, -9, 54}

But if I changed the elements to:

{-55, 45, 6, 11, 54}

There is an exception error. Here is my code:

public class ary1 {
    public static void sort(int A[], int n) {
        int tmp;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if (A[0] % 2 == 0) //even
                {
                    if (A[i] < A[j]) {
                        tmp = A[i];
                        A[i] = A[j];
                        A[j] = tmp;
                    }
                } else {
                    if (A[i] > A[j]) {
                        tmp = A[i];
                        A[i] = A[j];
                        A[j] = tmp;
                    }
                }
            }
        }
    }

    public static void showAray(int A[], int n) {
        for (int i = 0; i < n; i++) {
            System.out.println(A[i]);
        }
    }

    public static void main(String args[]) {
        int array1[] = {-55, 45, 6, 11, 54};
        int odd = 0;
        int even = 0;

        for (int i = 0; i < array1.length; i++) {
            if (array1[i] % 2 == 0) {
                even++;
            } else {
                odd++;
            }
        }

        int[] array2 = new int[even];
        int[] array3 = new int[odd];

        for (int i = 0, j = 0, k = 0; i < array1.length; i++) {
            if (array1[i] % 2 == 0) {
                array2[j++] = array1[i];
            } else {
                array3[k++] = array1[i];
            }
        }

        System.out.println("Original array:\n");
        showAray(array1, array1.length);

        sort(array2, even);
        sort(array3, odd);

        for (int i = 1; i < array1.length; i++) {
            if (i < odd) {
                array1[i] = array3[i];
            } else {
                array1[i] = array2[(i + 1) - even];
            }
        }

        System.out.println("\nAfter sorting:\n");
        showAray(array1, array1.length);
    }
}

I know there is a logical error here, but I can't figure out what exactly. Is there any way to change the logic to work with all integers? Thanks.

array1[i] = array2[(i + 1) - even];

EDIT - Here is the stacktrace.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at ary.main(arytest.java:67)
Java Result: 1
1
  • 2
    "There is an exception error." - please share the stacktrace with us and indicate the line in your code where the Exception occurs. Commented Sep 15, 2015 at 6:49

1 Answer 1

1

Change this

array1[i] = array2[(i + 1) - even];

to

array1[i] = array2[i - odd];

I guess this is what you want

Sign up to request clarification or add additional context in comments.

5 Comments

That's correct. But it would be better to explain why ;)
Nah, 1+1 is very easy to explain. Don't overthink it!
Please explain for me :)
I would if it wasn't a comment. Take two pictures of apples and here you go: one plus one and you've got yourself two apples :)))
Thanks, but that is not how we do in Math.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.