text: Sort the matrix column | Java program for the above approach ; Function to find the transpose of the matrix mat [ ] ; Stores the transpose of matrix mat [ ] [ ] ; Traverse each row of the matrix ; Traverse each column of the matrix ; Transpose matrix elements ; Function to sort the given matrix in row wise manner ; Traverse the row ; Row - Wise Sorting ; Function to print the matrix in column wise sorted manner ; Function call to find transpose of the the matrix mat [ ] [ ] ; Sorting the matrix row - wise ; Calculate transpose of B [ ] [ ] ; Print the matrix mat [ ] [ ] ; Driver Code ; Input ; Function call to print the matrix in column wise sorted manner
code: import java . util . * ; import java . util . Arrays ; class GFG { static int [ ] [ ] transpose ( int [ ] [ ] mat , int row , int col ) { int [ ] [ ] tr = new int [ col ] [ row ] ; for ( int i = 0 ; i < row ; i ++ ) { for ( int j = 0 ; j < col ; j ++ ) { tr [ j ] [ i ] = mat [ i ] [ j ] ; } } return tr ; } static void RowWiseSort ( int [ ] [ ] B ) { for ( int i = 0 ; i < ( int ) B . length ; i ++ ) { Arrays . sort ( B [ i ] ) ; } } static void sortCol ( int [ ] [ ] mat , int N , int M ) { int [ ] [ ] B = transpose ( mat , N , M ) ; RowWiseSort ( B ) ; mat = transpose ( B , M , N ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { System . out . print ( mat [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 1 , 6 , 10 } , { 8 , 5 , 9 } , { 9 , 4 , 15 } , { 7 , 3 , 60 } } ; int N = mat . length ; int M = mat [ 0 ] . length ; sortCol ( mat , N , M ) ; } }