-1

I have 2D array which I need to sort it's rows only, and I have done with this code:

double[][] arr = initArray();

    for (int row = 0; row < arr.length; row++) {
        int index = row;
        double minVal = arr[row][0];
        for (int column = 0; column < arr[row].length; column++) {

            if (arr[row][column] < minVal) {
                minVal = arr[row][column];
                index = column;
            }

        }


        arr[row][index] = arr[row][0];
        arr[row][0] = minVal;

    }

Sample input:

{{0.15, 0.875, 0.375},
 {0.55, 0.005, 0.225},
 {0.30, 0.12, 0.4}}

Expect out put:

0.15 0.375 0.875 
0.005 0.225 0.55 
0.12 0.3 0.4

What I get:

0.15 0.875 0.375 
0.005 0.55 0.225 
0.12 0.3 0.4
  • 2d array is array of arrays `{ {...}, {...}, {...} }`. Your current code doesn't sort internal arrays, it is searching for minimum in each row and moves it at start of that row. Try to think of way to sort 1d array and then apply it to each row. – Pshemo Nov 08 '17 at 00:19
  • @Pshemo Thanks for pointing that. –  Nov 08 '17 at 00:24
  • @jontro thanks for pointing that, but it's not duplication here, I said `rows`. –  Nov 08 '17 at 00:37

3 Answers3

1

You can use the Arrays.sort(); function from the java.util.Arrays class to sort each row in your 2d Array.

 for (double[] innerArray: outerArray) {
     Arrays.sort(innerArray);
 }

EDIT: Here is the entire example:

double[][] outerArray = {
     {
         0.15,
         0.875,
         0.375
     },
     {
         0.55,
         0.005,
         0.225
     },
     {
         0.30,
         0.12,
         0.4
     }
 };


 for (double[] innerArray: outerArray) {
     Arrays.sort(innerArray);
 }


 for (int i = 0; i < outerArray.length; i++) {

     for (int j = 0; j < outerArray.length; j++) {

         System.out.print(outerArray[i][j] + " ");

     }
     System.out.println();

 }
0

As @Pshemo mentioned, you didn't go through all your values, here is a prob way:

 for (int row = 0; row < arr.length; row++) {
        for (int column = 0; column < arr.length - 1; column++) {
            double min = arr[row][column];
            int index = column;
            for (int j = column + 1; j < arr.length; j++) {
                if (min > arr[row][j]) {
                    min = arr[row][j];
                    index = j;
                }
            }
            if (index != column) {
                arr[row][index] = arr[row][column];
                arr[row][column] = min;
            }
        }
    }
0
        for (int column = 0; column < arr[row].length; column++) {
        //**insert another for loop here**
            if (arr[row][column] < minVal) {
              minVal = arr[row][column];
              index = column;
            }

        }

Your code is running once per row, and hence it is only finding the smallest and shifting to first position. You just have to make sure that the same action happens as many times as there ar items in the row. Look here for reference Bubble Sort

inquizitive
  • 622
  • 4
  • 21