james
james

Reputation: 55

program wont sort array after one previous sorting

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.

Upvotes: 0

Views: 73

Answers (3)

Davide Spataro
Davide Spataro

Reputation: 7482

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]

Upvotes: 3

Все Едно
Все Едно

Reputation: 726

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

Upvotes: 0

dclifford
dclifford

Reputation: 27

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.

Upvotes: 0

Related Questions