Sort the given matrix | JavaScript implementation to sort the given matrix ; function to sort the given matrix ; temporary matrix of size n ^ 2 ; copy the elements of matrix one by one into temp [ ] ; sort temp [ ] ; copy the elements of temp [ ] one by one in mat [ ] [ ] ; function to print the given matrix ; Driver program to test above
let SIZE = 10 function sortMat ( mat , n ) { let temp = new Array ( n * n ) ; let k = 0 ; for ( let i = 0 ; i < n ; i ++ ) for ( let j = 0 ; j < n ; j ++ ) temp [ k ++ ] = mat [ i ] [ j ] ; temp . sort ( ) ; k = 0 ; for ( let i = 0 ; i < n ; i ++ ) for ( let j = 0 ; j < n ; j ++ ) mat [ i ] [ j ] = temp [ k ++ ] ; } function printMat ( mat , n ) { for ( let i = 0 ; i < n ; i ++ ) { for ( let j = 0 ; j < n ; j ++ ) document . write ( mat [ i ] [ j ] + " " ) ; document . write ( " " ) ; } } let mat = [ [ 5 , 4 , 7 ] , [ 1 , 3 , 8 ] , [ 2 , 9 , 6 ] ] ; let n = 3 ; document . write ( " " + " " ) ; printMat ( mat , n ) ; sortMat ( mat , n ) ; document . write ( " " ) ; document . write ( " " + " " ) ; printMat ( mat , n ) ;