Sort the major diagonal of the matrix | Java implementation to sort the major diagonal of the matrix ; Function to sort the major diagonal of the matrix ; Loop to find the ith minimum element from the major diagonal ; Loop to find the minimum element from the unsorted matrix ; Swap to put the minimum element at the beginning of the major diagonal of matrix ; Loop to print the matrix ; Driven Code ; Sort the major Diagonal
class GFG { static void sortDiagonal ( int a [ ] [ ] , int M , int N ) { for ( int i = 0 ; i < M ; i ++ ) { int sm = a [ i ] [ i ] ; int pos = i ; for ( int j = i + 1 ; j < N ; j ++ ) { if ( sm > a [ j ] [ j ] ) { sm = a [ j ] [ j ] ; pos = j ; } } swap ( a , i , i , pos , pos ) ; } for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) System . out . print ( a [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } static void swap ( int [ ] [ ] a , int i , int i2 , int pos , int pos2 ) { int temp = a [ i ] [ i2 ] ; a [ i ] [ i2 ] = a [ pos ] [ pos2 ] ; a [ pos ] [ pos2 ] = temp ; } public static void main ( String [ ] args ) { int a [ ] [ ] = { { 4 , 2 } , { 3 , 1 } } ; sortDiagonal ( a , 2 , 2 ) ; } }