Sorting boundary elements of a matrix | Javascript program for the above approach ; Appending border elements ; Sorting the list ; Printing first row with first N elements from A ; Printing N - 2 rows ; Print elements from last ; Print middle elements from original matrix ; Print elements from front ; Printing last row ; Dimensions of a Matrix ; Given Matrix ; Function Call
function printMatrix ( grid , m , n ) { let A = [ ] ; for ( let i = 0 ; i < m ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) { if ( j == n - 1 || ( i == m - 1 ) j == 0 i == 0 ) A . push ( grid [ i ] [ j ] ) ; } } A . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i < n ; i ++ ) document . write ( A [ i ] + " " ) ; document . write ( " " ) for ( let i = 0 ; i < m - 2 ; i ++ ) { document . write ( A [ A . length - i - 1 ] + " " ) ; for ( let j = 1 ; j < n - 1 ; j ++ ) document . write ( grid [ i + 1 ] [ j ] + " " ) ; document . write ( A [ n + i ] + " " ) } document . write ( A . slice ( n + m - 2 , n + m - 2 + n ) . reverse ( ) . join ( " " ) ) ; [ n + m - 2 : n + m - 2 + n ] << endl ; } let m = 4 , n = 5 ; let grid = [ [ 1 , 2 , 3 , 4 , 0 ] , [ 1 , 1 , 1 , 1 , 2 ] , [ 1 , 2 , 2 , 2 , 4 ] , [ 1 , 9 , 3 , 1 , 7 ] ] ; printMatrix ( grid , m , n ) ;