0

I have to sort elements in each row and then display the array.

For example, if the input array is:

             5 1 3                  1 3 5
   INPUT:    7 6 4        OUTPUT:   4 6 7
             9 8 2                  2 8 9

My code for that was:

for (int i = 0; i < size; i++) { //"size" is the size of the square matrix 
    for (int j = 0; j < size; j++) {
        for (int k = 0; k < size - 1; k++) {
            for (int l = 0; l < size - k - 1; l++) {
                if (arr[i][j] > arr[i][j+1]) { //arr[][] is of datatype int
                    int temp = arr[i][j];
                    arr[i][j] = arr[i][j+1];
                    arr[i][j+1] = temp;
                }
            }
        }
    }
}

Any suggestions?

5
  • 4
    It looks homework. You should do on your own... Commented Dec 28, 2013 at 7:23
  • Is your code not working? If not, what output is it producing? (Thanks for providing expected output!) Commented Dec 28, 2013 at 7:27
  • do you have to implement your own sorting algorithm???... Can you use inbuilt sort methods??? Commented Dec 28, 2013 at 7:27
  • docs.oracle.com/javase/7/docs/api/java/util/… Commented Dec 28, 2013 at 7:31
  • I have to implement own sorting methods, and the output it is producing is: 6 7 3 6 7 4 9 8 2 Commented Dec 28, 2013 at 7:33

3 Answers 3

5
for (int i = 0; i < size; i++){ //"size" is the size of the square matrix 
    for (int j = 0; j < size; j++){
        for (int k = j+1; k < size; k++){
           if (arr[i][j] > arr[i][k]){ //arr[][] is of datatype int
                  int temp  =  arr[i][j];
                  arr[i][j] =  arr[i][k];
                  arr[i][k] =  temp;
            }

         }
     }
}

I don't think you need 4th loop

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

4 Comments

um, what were your loop variables?
@user3140017 loop variables not understood what you are asking?
oh, sorry about that, my internet connection had a prob, so couldn't see the full code of yours. And, yes its working, Thank You very Much!! Can u please tell me how u came up with this logic?
@user3140017 Most importantly, really spend some time and make sure you fully understand this simple sorting algorithm. Maybe even diagram each step on a piece paper...
4

I would do it simpler

    for(int[] r : arr){
        Arrays.sort(r);
    }

1 Comment

It's homework so he can't use Arrays.sort(array). But this idea of sorting one row at a time is definitely the better solution.
0

I would create a method to sort rows, and then just iterate through the rows in the matrix and sort them one at a time. For example:

public static int[] sortRow(int[] row) // selection sort
{
    for (int i = 0; i < row.length - 1; i++) {
        for (int j = i + 1; j < row.length; j++) {
            if (row[i] > row[j]) {
                int temp = row[i];
                row[i] = row[j];
                row[j] = temp;
            }
        }
    }
    return row;
}

public static void main(String args[]) 
{
    int[][] arr = {{5, 1, 3}, {7,6,4}, {9,8,2}};

    for (int r = 0; r < arr.length; r++) { // for every row in the matrix
        arr[r] = sortRow(arr[r]); // set the row to be the sorted row
    }

    // print out the array to the console
    for (int r[] : arr) {
        for (int c : r)
            System.out.print(c + " ");
        System.out.println();
    }
}

Output:

1 3 5 
4 6 7 
2 8 9 

Comments

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.