patriciasmh
patriciasmh

Reputation: 350

Sorting rows of a 2d integer array from smallest to biggest

I have 2D integer array named arry like this:

[6, 2, 7]
[3, 6, 7]
[5, 6, 1]
[5, 3, 4]
[5, 3, 8]

I want to sort it in the way this would be the result (in order to do so I create new array of the same size, named table):

[3, 6, 7]
[5, 6, 1]
[5, 3, 4]
[5, 3, 8]
[6, 2, 7]

And I have this code:

for (int k = 0; k < numOfArrays; k++) {
    int smallest = 2147483647;
    int indexSmallest = 0;
    for (int h = 0; h < numOfArrays; h++) {
        if (arry[h][0] < smallest) {
            smallest = arry[h][0];
            indexSmallest = h;
        }
    }
    tabel[k] = arry[indexSmallest];
    arry[indexSmallest][0] = 2147483647;
}

for (int k = 0; k < numOfArrays; k++) {
    System.out.println(Arrays.toString(tabel[k]));
}

The result is:

[2147483647, 6, 7]
[2147483647, 6, 1]
[2147483647, 3, 4]
[2147483647, 3, 8]
[2147483647, 2, 7]

I don't understand how can table contain 2147483647, if I've never set any value of table to 2147483647?

Upvotes: 2

Views: 300

Answers (5)

user14940971
user14940971

Reputation:

You can greatly simplify your code by using Arrays.sort method, or otherwise you can implement something like this.


Selection sort of a 2d array by column

public static void selectionSort2D(int[][] arr, int column) {
    // iterate over all subsets of the rows of the array
    // (0-last, 1-last, 2-last, 3-last, ...)
    for (int i = 0; i < arr.length; i++) {
        // assume the min is the first row element
        int min = arr[i][column];
        // row index of the min element
        int min_i = i;
        // check the rows after i to find the smallest
        for (int j = i + 1; j < arr.length; j++) {
            // if this row element is less,
            // then it is the new min
            if (arr[j][column] < min) {
                min = arr[j][column];
                min_i = j;
            }
        }
        // if the min element row is not equal to
        // the current one, then swap these rows
        if (i != min_i) {
            int[] temp = arr[i];
            arr[i] = arr[min_i];
            arr[min_i] = temp;
        }
    }
}
// test
public static void main(String[] args) {
    int[][] arr = {
            {6, 2, 7},
            {3, 6, 7},
            {5, 6, 1},
            {5, 3, 4},
            {5, 3, 8}};

    // sort by first column
    selectionSort2D(arr, 0);

    // output
    for (int[] row : arr)
        System.out.println(Arrays.toString(row));
    //[3, 6, 7]
    //[5, 6, 1]
    //[5, 3, 4]
    //[5, 3, 8]
    //[6, 2, 7]
}

Upvotes: 0

gknicker
gknicker

Reputation: 5568

Remember, Java arrays are objects, which means they are passed by reference. So you're not actually making a copy of the inner arrays; use Arrays.copyOf() for that.

If you're allowed to use Arrays.sort() with a Comparator, you could do it like this.

java.util.Arrays.sort(arry, new java.util.Comparator<int[]>() {
    public int compare(int[] a1, int[] a2) {
        for (int k = 0; k < a1.length; k++) {
            if (a1[k] != a2[k]) {
                return a1[k] - a2[k];
            }
        }
        return 0;
    }
});
for (int k = 0; k < numOfArrays; k++) {
    System.out.println(java.util.Arrays.toString(arry[k]));
}

If that's not allowed, then you can still use the comparison logic inside the above Comparator.compare() method. That's your basic sorting logic regardless of the implementation details.

Upvotes: 1

Yves Dubois
Yves Dubois

Reputation: 943

The real problem is this line:

tabel[k] = arry[indexSmallest];

Remember, arrays are objects. This line doesn't copy the inner array, it sets a reference to it.

So at this point both tabel[k] and arry[indexSmallest] point to the same array object. So when you do this:

arry[indexSmallest][0] = 2147483647;

You change it for both arry[indexSmallest] and tabel[k] (since they point to the same object)

To fix the problem, assing to tabel[k] a copy of the array:

tabel[k] = Arrays.copyOf(arry[indexSmallest], 3);

Upvotes: 2

roeygol
roeygol

Reputation: 5048

You have make this info inside of an Object and implement Comparable/Comparator:

public YourObject implements Comparable {
    private long a;
    private long b;
    private long c;

    public YourObject (long a, long b, long c){
         this.a = a;
         this.b = b;
         this.c = c;
    }

    public int compareTo(Object arg0) {
        //make your comparison here...
    }

}

In main function: YourObject[] arr = new YourObject[3]; arr[0] = new YourObject (.., .., ..,); arr[1] = new YourObject (.., .., ..,); arr[2] = new YourObject (.., .., ..,);

Arrays.sort(arr); //sorting your data

Upvotes: 0

Adam P
Adam P

Reputation: 4713

arry[indexSmallest][0] = 2147483647;

This line of code is setting the first element in each row to 2147483647 at the end of your outermost for loop. This is why it appears in each row. I'm not sure what you intended to do here, but this is why you are getting that value.

Upvotes: 2

Related Questions