Sort the given Matrix | Memory Efficient Approach | Javascript implementation to sort the given matrix in strict order ; Function to sort the matrix ; Number of elements in matrix ; Loop to sort the matrix using Bubble Sort ; Condition to check if the Adjacent elements ; Swap if previous value is greater ; Loop to print the matrix ; Driver Code ; Function call to sort ; Function call to print matrix
let N = 3 ; let M = 3 ; function sortMat ( data , row , col ) { let size = row * col ; for ( let i = 0 ; i < size ; i ++ ) { for ( let j = 0 ; j < size - 1 ; j ++ ) { if ( data [ Math . floor ( j / col ) ] [ j % col ] > data [ Math . floor ( ( j + 1 ) / col ) ] [ ( j + 1 ) % col ] ) { let temp = data [ Math . floor ( j / col ) ] [ j % col ] ; data [ Math . floor ( j / col ) ] [ j % col ] = data [ Math . floor ( ( j + 1 ) / col ) ] [ ( j + 1 ) % col ] ; data [ Math . floor ( ( j + 1 ) / col ) ] [ ( j + 1 ) % col ] = temp ; } } } } function printMat ( mat , row , col ) { for ( let i = 0 ; i < row ; i ++ ) { for ( let j = 0 ; j < col ; j ++ ) { document . write ( mat [ i ] [ j ] + " " ) ; } document . write ( " " ) ; } } let mat = [ [ 5 , 4 , 7 ] , [ 1 , 3 , 8 ] , [ 2 , 9 , 6 ] ] ; let row = N ; let col = M ; sortMat ( mat , row , col ) ; printMat ( mat , row , col ) ;