text: Sorting rows of matrix in descending order followed by columns in ascending order | C # implementation to sort the rows of matrix in descending order followed by sorting the columns in ascending order ; function to sort each row of the matrix according to the order specified by descending . ; function to sort each row of the matrix ; sorting row number ' i ' ; function to find transpose of the matrix ; swapping element at index ( i , j ) by element at index ( j , i ) ; function to sort the matrix row - wise and column - wise ; sort rows of [ , ] mat in descending order ; get transpose of [ , ] mat ; again sort rows of [ , ] mat in ascending order . ; again get transpose of [ , ] mat ; function to print the matrix ; Driver code
code: using System ; class GFG { static int MAX_SIZE = 10 ; static void sortByRow ( int [ , ] mat , int n , bool descending ) { int temp = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( descending == true ) { int t = i ; for ( int p = 0 ; p < n ; p ++ ) { for ( int j = p + 1 ; j < n ; j ++ ) { if ( mat [ t , p ] < mat [ t , j ] ) { temp = mat [ t , p ] ; mat [ t , p ] = mat [ t , j ] ; mat [ t , j ] = temp ; } } } } else sortByRow ( mat , i , n ) ; } } static void sortByRow ( int [ , ] mat , int row , int n ) { for ( int i = row ; i < row + 1 ; i ++ ) { for ( int j = 0 ; j < n - 1 ; j ++ ) { if ( mat [ i , j ] > mat [ i , j + 1 ] ) { var temp = mat [ i , j ] ; mat [ i , j ] = mat [ i , j + 1 ] ; mat [ i , j + 1 ] = temp ; } } } } static void transpose ( int [ , ] mat , int n ) { int temp = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { temp = mat [ i , j ] ; mat [ i , j ] = mat [ j , i ] ; mat [ j , i ] = temp ; } } } static void sortMatRowAndColWise ( int [ , ] mat , int n ) { sortByRow ( mat , n , true ) ; transpose ( mat , n ) ; sortByRow ( mat , n , false ) ; transpose ( mat , n ) ; } static void printMat ( int [ , ] mat , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) Console . Write ( mat [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { int n = 3 ; int [ , ] mat = { { 3 , 2 , 1 } , { 9 , 8 , 7 } , { 6 , 5 , 4 } } ; Console . WriteLine ( " Original ▁ Matrix : " ) ; printMat ( mat , n ) ; sortMatRowAndColWise ( mat , n ) ; Console . WriteLine ( " STRNEWLINE Matrix ▁ After ▁ Sorting : " ) ; printMat ( mat , n ) ; } }