0

I have an exercise where I need to sort a matrix. The matrix should be sorted so that a smaller number would be in the first row on the left side of the matrix and so on ...

Until now I have written code to sort the rows in a matrix and the columns but I can not sort out all the matrix.

Clarification: Do not use a one-dimensional array helped.

I think I'm almost done I'd love to help

import java.util.Scanner;

public class matSortingCol {
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    System.out.println("Enter length of mat:");
    int length = in.nextInt();
    System.out.println("Enter search number: ");
    int num = in.nextInt();

    int[][] mat = new int[length][length];
    int[] arrSorting = new int[length];

    for (int i = 0; i < mat.length; i++) {
        for (int j = 0; j < mat.length; j++) {
            mat[i][j] = in.nextInt();
        }
    }
    System.out.println("Orginal matrix: ");
    // print matrix
    for (int i = 0; i < mat.length; i++) {
        for (int j = 0; j < mat.length; j++) {
            System.out.print("[" + mat[i][j] + "]");
        }
        System.out.println();
    }

    for (int j = 0; j < mat.length; j++) {
        for (int i = 0; i < mat.length; i++) {
            arrSorting[i] = mat[i][j];
        }// end for i
        sorting(arrSorting);
        System.out.println();
        for (int i = 0; i < mat.length; i++) {
            mat[i][j] = arrSorting[i];
        }// end for i

    }// end for j

    System.out.println();
    // print matrix
    System.out.println("matrix after sorting: ");
    for (int i = 0; i < mat.length; i++) {
        for (int j = 0; j < mat.length; j++) {
            System.out.print("[" + mat[i][j] + "]");
        }// end for j
        System.out.println();
    }// end for i

    for (int i = 0; i < mat.length; i++) {
        for (int j = 0; j < mat.length; j++) {
            if (num == mat[i][j]) {
                int search = 0;
                search = mat[i][j];

                System.out.print("Find number " + "[" + search + "]"
                        + " >> " + "index of row is: " + "[" + i + "]"
                        + ", " + "index of col is: " + "[" + j + "]");

                System.out.println();
            }

        }

    }

}// end main

public static void sorting(int[] arr) {
    for (int i = 0; i < arr.length - 1; i++) {
        int min = i;

        for (int j = i + 1; j < arr.length; j++) {
            if (arr[j] < arr[min]) {
                min = j;
            }// end if
        }// end j
        if (i != min) {
            int swap = arr[i];
            arr[i] = arr[min];
            arr[min] = swap;

        }// end if
    }// end i
    System.out.print("The arr after sotring: ");
    for (int i = 0; i < arr.length; i++) {
        System.out.print("[" + arr[i] + "]");
    }
}// end sorting
}// end class

Thank you

Liran
  • 37
  • 1
  • 4
  • so you want the matrix to be row wise sorted? – Deendayal Garg May 06 '16 at 08:18
  • Easiest way: Selection sort considering all the elements in the matrix. More efficient way: http://stackoverflow.com/questions/2571049/how-to-sort-in-place-using-the-merge-sort-algorithm (just modify the accesses to treat the matrix as one-dimensional structure) – fabian May 06 '16 at 08:24
  • Where does the second smallest element go? Row 1 column 2 or row 2 column 1? Are all elements sorted at once or are only elements of the same row/column sorted within the same row/column? If the smallest element were in the last row and last column, would it then have to be relocated to the first row and first column? –  May 06 '16 at 08:33
  • You have to be careful when looping over a n-dimensional array. mat.length will give you the length of a row (from conceptual point of view), but if you want the length of a column, you have to grap it by mat[i].length. Also, it is not completely clear, but I assume that you shall sort the matrix from left to right and then from up to down? – Supahupe May 06 '16 at 08:41

0 Answers0