0

This is what i have so far:

import java.util.*;
public class ProcessArray {

    public static void main(String[] args) {

        int[] arr = {3, 8, 1, 9, 2};
        System.out.println(Arrays.toString(arr));
        selectionSort(arr);
        System.out.println(Arrays.toString(arr));
        ProcessArray.oddArray(arr);
        System.out.println(Arrays.toString(arr));
        ProcessArray.evenArray(arr);
        System.out.println(Arrays.toString(arr));

    }

    public static void selectionSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int index = 0;
            for (int k = 0; k < arr.length - i; k++) {
                if (arr[k] > arr[index]) {
                    index = k;
                }
            }
            //swap
            int tmp = arr[index];
            arr[index] = arr[arr.length - 1 - i];
            arr[arr.length - 1 - i] = tmp;
        }
    }

    public static int[] oddArray(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] % 2 == 0) {
                arr[i] = arr[i];
            }
        }
        return arr;
    }

    public static int[] evenArray(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] % 2 != 0) {
                arr[i] = arr[i];
            }
        }
        return arr;
    }

}

For some reason when i go to run it, this is what im outputted with:

[3, 8, 1, 9, 2]
[1, 2, 3, 8, 9]
[1, 2, 3, 8, 9]
[1, 2, 3, 8, 9]

How do i make odd Array and even Array work? Because the logic make sense, at least to me it does, but the program still just prints the output it got from selectionSort. Im sorry if this basic computer science knowledge, we just recently started learning about sorting.

5
  • Perhaps you would like to fix your indentation if you are asking people to read your code. Commented Jul 6, 2017 at 14:39
  • What is the expected output? Commented Jul 6, 2017 at 14:42
  • 2
    arr[i] = arr[i] won't change anything. Commented Jul 6, 2017 at 14:43
  • You are using the array reference, instead copy arrays Commented Jul 6, 2017 at 14:43
  • the hint is on the method definition : public static int[] evenArray(int[] arr) to me means: return a new array that only contains the even numbers of arr Commented Jul 6, 2017 at 15:03

3 Answers 3

3

Well, there are few problems with your code.

  • arr[i] = arr[i] does not make sense. Since arr is passed by reference to ***Array function it means that arr inside the main and arr inside oddArray are exactly the same array (same size and elements). So when you loop over testing for %2!=0 and do arr[i]=arr[i] you are not really changing the content of arr at all.

  • odd test should be %2!=0 and even %2==0

Now What I think you want to do is to filter the odd and even numbers from the original array. What you want to do is to count the number of odd numbers first, then create a new array of the size equal to the counter and then put only the elements of arr that are odd inside the new array.

Something as the following should work:

public static void main(String[] args) {

        int[] arr = {3, 8, 1, 9, 2};
        int[] res;
        System.out.println(Arrays.toString(arr));
        selectionSort(arr);
        System.out.println(Arrays.toString(arr));
        res = Test.oddArray(arr);
        System.out.println(Arrays.toString(res));
        res=Test.evenArray(arr);
        System.out.println(Arrays.toString(res));

    }


    public static int[] oddArray(int[] arr) {
        int c = 0;
        for (int i = 0; i < arr.length; i++) 
            if (arr[i] % 2 != 0) 
                c++;
         //c contains the number of odd number in arr
        int[] arr_c = new int[c];
        int ins=0;
        for (int i = 0; i < arr.length; i++) 
            if (arr[i] % 2 != 0)
                arr_c[ins++]=arr[i];
        return arr_c;
    }

    public static int[] evenArray(int[] arr) {
        int c = 0;
        for (int i = 0; i < arr.length; i++) 
            if (arr[i] % 2 == 0) 
                c++;

        int[] arr_c = new int[c];
        int ins=0;
        for (int i = 0; i < arr.length; i++) 
            if (arr[i] % 2 == 0)
                arr_c[ins++]=arr[i];
        return arr_c;
    }

which outputs

[3, 8, 1, 9, 2]
[1, 2, 3, 8, 9]
[1, 3, 9]
[2, 8]
Sign up to request clarification or add additional context in comments.

Comments

0

You are trying to sort the array after it has already been sorted. Pass in a copy of the original array to the sorting methods instead of the original array reference.

Comments

0

Here now it works

  import java.util.*;
    public class ProcessArray {

        public static void main(String[] args) {

            int[] arr = {3, 8, 1, 9, 2};
            System.out.println(Arrays.toString(arr));
            selectionSort(arr);
            System.out.println(Arrays.toString(arr));
            System.out.println((ProcessArray.oddArray(arr)));
            System.out.println( ProcessArray.evenArray(arr));

        }

        public static void selectionSort(int[] arr) {
            for (int i = 0; i < arr.length - 1; i++) {
                int index = 0;
                for (int k = 0; k < arr.length - i; k++) {
                    if (arr[k] > arr[index]) {
                        index = k;
                    }
                }
                //swap
                int tmp = arr[index];
                arr[index] = arr[arr.length - 1 - i];
                arr[arr.length - 1 - i] = tmp;
            }
        }

        public static List<Integer> oddArray(int[] arr) {
            List<Integer> oddArr = new ArrayList<>();
            for (int i = 0; i < arr.length; i++) {
                if (arr[i] % 2 != 0) {
                  oddArr.add(arr[i]);
                }
            }
            return oddArr;
        }

        public static List<Integer> evenArray(int[] arr) {
            List<Integer> evenArray = new ArrayList<>();
            for (int i = 0; i < arr.length; i++) {
                if (arr[i] % 2 == 0) {
                    evenArray.add(arr[i]);
                }
            }
            return evenArray;
        }

    }

Your have to use List<> since you dont know the length of your collection, you dont know how many odd/even numbers there will be

2nd: you methods are wrong

   public static int[] evenArray(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] % 2 != 0) {
                arr[i] = arr[i];
            }
        }
        return arr;
    }

Here if you can see on arr[i] = arr[i]; So you are saying in the same array that i passed in to the method if lets say if arr[3] is even make arr[3] = arr[3] It's a very basic mistake that i made my self as well in the beggining

5 Comments

mate, I would strongly disagree on changing method definition. changing int[] to List<Integer> can break the whole system (imagine this method is part of a big package)
first of all if we don't know the length of something we can't use array to store it. Unless you make new array[Integer.MaxValue()],, but that's retarded, second look at his code and mistakes. We have to try to explain it as simple as possible, he is on a basic lvl. Third of all I mean check the answer of Davide Spataro, he ittarates the collection once just see the count of numbers, just to make the array have appropriet size, and then itterates over it again .... If you think that's better. I wont argue with you any more.
mate, you took my argument in a wrong way, of course in terms of efficiency your code is better . I would do as you did if I was the software architecture. my argument is that you are changing the definition of the method. its a bad practice. changing definition of a code means changing potentially many many parts of an actual program.
Yes it's better to add a new method then to change an old one, but he is just trying to learn collections and basic java and i believe this will help him more, if he googles what a List is and see when you use list and when to use array. And just in theory if we assume it's a part of a bigger program, if that's you data handling you are dead :D
mate, you still don't get my point, beside, I can produce a method that returns int[] which outperforms your method.

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.