Row wise sorting in 2D array | C # code to sort 2D matrix row - wise ; loop for rows of matrix ; loop for column of matrix ; loop for comparison and swapping ; swapping of elements ; printing the sorted matrix ; Driver Code
using System ; class GFG { static int sortRowWise ( int [ , ] m ) { for ( int i = 0 ; i < m . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < m . GetLength ( 1 ) ; j ++ ) { for ( int k = 0 ; k < m . GetLength ( 1 ) - j - 1 ; k ++ ) { if ( m [ i , k ] > m [ i , k + 1 ] ) { int t = m [ i , k ] ; m [ i , k ] = m [ i , k + 1 ] ; m [ i , k + 1 ] = t ; } } } } for ( int i = 0 ; i < m . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < m . GetLength ( 1 ) ; j ++ ) Console . Write ( m [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } return 0 ; } public static void Main ( String [ ] args ) { int [ , ] m = { { 9 , 8 , 7 , 1 } , { 7 , 3 , 0 , 2 } , { 9 , 5 , 3 , 2 } , { 6 , 3 , 1 , 2 } } ; sortRowWise ( m ) ; } }