Given a n x n matrix. The problem is to sort the matrix row-wise and column wise.
Examples:
Input : mat[][] = { {4, 1, 3},
{9, 6, 8},
{5, 2, 7} }
Output : 1 3 4
2 5 7
6 8 9
Input : mat[][] = { {12, 7, 1, 8},
{20, 9, 11, 2},
{15, 4, 5, 13},
{3, 18, 10, 6} }
Output : 1 5 8 12
2 6 10 15
3 7 11 18
4 9 13 20
Approach: Following are the steps:
- Sort each row of the matrix.
- Get transpose of the matrix.
- Again sort each row of the matrix.
- Again get transpose of the matrix.
Algorithm for sorting each row of matrix using C++ STL sort():
for (int i = 0 ; i < n; i++)
sort(mat[i], mat[i] + n);
Algorithm for getting transpose of the matrix:
for (int i = 0; i < n; i++) {
for (int j = i + 1; i < n; i++) {
int temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX_SIZE 10
void sortByRow( int mat[MAX_SIZE][MAX_SIZE], int n)
{
for ( int i = 0; i < n; i++)
sort(mat[i], mat[i] + n);
}
void transpose( int mat[MAX_SIZE][MAX_SIZE], int n)
{
for ( int i = 0; i < n; i++)
for ( int j = i + 1; j < n; j++)
swap(mat[i][j], mat[j][i]);
}
void sortMatRowAndColWise( int mat[MAX_SIZE][MAX_SIZE],
int n)
{
sortByRow(mat, n);
transpose(mat, n);
sortByRow(mat, n);
transpose(mat, n);
}
void printMat( int mat[MAX_SIZE][MAX_SIZE], int n)
{
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++)
cout << mat[i][j] << " " ;
cout << endl;
}
}
int main()
{
int mat[MAX_SIZE][MAX_SIZE] = { { 4, 1, 3 },
{ 9, 6, 8 },
{ 5, 2, 7 } };
int n = 3;
cout << "Original Matrix:\n" ;
printMat(mat, n);
sortMatRowAndColWise(mat, n);
cout << "\nMatrix After Sorting:\n" ;
printMat(mat, n);
return 0;
}
|
Java
import java.util.Arrays;
class GFG
{
static final int MAX_SIZE= 10 ;
static void sortByRow( int mat[][], int n)
{
for ( int i = 0 ; i < n; i++)
Arrays.sort(mat[i]);
}
static void transpose( int mat[][], int n)
{
for ( int i = 0 ; i < n; i++)
for ( int j = i + 1 ; j < n; j++)
{
int temp=mat[i][j];
mat[i][j]=mat[j][i];
mat[j][i]=temp;
}
}
static void sortMatRowAndColWise( int mat[][], int n)
{
sortByRow(mat, n);
transpose(mat, n);
sortByRow(mat, n);
transpose(mat, n);
}
static void printMat( int mat[][], int n)
{
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < n; j++)
System.out.print(mat[i][j] + " " );
System.out.println();
}
}
public static void main (String[] args)
{
int mat[][] = { { 4 , 1 , 3 },
{ 9 , 6 , 8 },
{ 5 , 2 , 7 } };
int n = 3 ;
System.out.print( "Original Matrix:\n" );
printMat(mat, n);
sortMatRowAndColWise(mat, n);
System.out.print( "\nMatrix After Sorting:\n" );
printMat(mat, n);
}
}
|
Python 3
MAX_SIZE = 10
def sortByRow(mat, n):
for i in range (n):
for j in range (n - 1 ):
if mat[i][j] > mat[i][j + 1 ]:
temp = mat[i][j]
mat[i][j] = mat[i][j + 1 ]
mat[i][j + 1 ] = temp
def transpose(mat, n):
for i in range (n):
for j in range (i + 1 , n):
t = mat[i][j]
mat[i][j] = mat[j][i]
mat[j][i] = t
def sortMatRowAndColWise(mat, n):
sortByRow(mat, n)
transpose(mat, n)
sortByRow(mat, n)
transpose(mat, n)
def printMat(mat, n):
for i in range (n):
for j in range (n):
print ( str (mat[i][j] ), end = " " )
print ();
mat = [[ 4 , 1 , 3 ],
[ 9 , 6 , 8 ],
[ 5 , 2 , 7 ]]
n = 3
print ( "Original Matrix:" )
printMat(mat, n)
sortMatRowAndColWise(mat, n)
print ( "\nMatrix After Sorting:" )
printMat(mat, n)
|
C#
using System;
class GFG
{
static void sortByRow( int [,]mat,
int n)
{
for ( int i = 0; i < n ; i++)
{
for ( int j = 0;
j < n - 1; j++)
{
if (mat[i, j] > mat[i, j + 1])
{
var temp = mat[i, j];
mat[i, j] = mat[i, j + 1];
mat[i, j + 1] = temp;
}
}
}
}
static void transpose( int [,]mat,
int n)
{
for ( int i = 0; i < n; i++)
for ( int j = i + 1;
j < n; j++)
{
var temp = mat[i, j];
mat[i, j] = mat[j, i];
mat[j, i] = temp;
}
}
static void sortMatRowAndColWise( int [,]mat,
int n)
{
sortByRow(mat, n);
transpose(mat, n);
sortByRow(mat, n);
transpose(mat, n);
}
static void printMat( int [,]mat, int n)
{
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
Console.Write(mat[i, j] + " " );
Console.Write( "\n" );
}
}
public static void Main ()
{
int [,]mat = {{4, 1, 3},
{9, 6, 8},
{5, 2, 7}};
int n = 3;
Console.Write( "Original Matrix:\n" );
printMat(mat, n);
sortMatRowAndColWise(mat, n);
Console.Write( "\nMatrix After Sorting:\n" );
printMat(mat, n);
}
}
|
PHP
<?php
$MAX_SIZE = 10;
function sortByRow(& $mat , $n )
{
for ( $i = 0; $i < $n ; $i ++)
sort( $mat [ $i ]);
}
function transpose(& $mat , $n )
{
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = $i + 1;
$j < $n ; $j ++)
{
$t = $mat [ $i ][ $j ];
$mat [ $i ][ $j ] = $mat [ $j ][ $i ];
$mat [ $j ][ $i ] = $t ;
}
}
}
function sortMatRowAndColWise(& $mat , $n )
{
sortByRow( $mat , $n );
transpose( $mat , $n );
sortByRow( $mat , $n );
transpose( $mat , $n );
}
function printMat(& $mat , $n )
{
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j < $n ; $j ++)
echo $mat [ $i ][ $j ] . " " ;
echo "\n" ;
}
}
$mat = array ( array ( 4, 1, 3 ),
array ( 9, 6, 8 ),
array ( 5, 2, 7 ));
$n = 3;
echo "Original Matrix:\n" ;
printMat( $mat , $n );
sortMatRowAndColWise( $mat , $n );
echo "\nMatrix After Sorting:\n" ;
printMat( $mat , $n );
?>
|
Javascript
<script>
let MAX_SIZE=10;
function sortByRow(mat,n)
{
for (let i = 0; i < n; i++)
mat[i].sort( function (a,b){ return a-b;});
}
function transpose(mat,n)
{
for (let i = 0; i < n; i++)
for (let j = i + 1; j < n; j++)
{
let temp=mat[i][j];
mat[i][j]=mat[j][i];
mat[j][i]=temp;
}
}
function sortMatRowAndColWise(mat,n)
{
sortByRow(mat, n);
transpose(mat, n);
sortByRow(mat, n);
transpose(mat, n);
}
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( "<br>" );
}
}
let mat = [[ 4, 1, 3 ],
[ 9, 6, 8 ],
[ 5, 2, 7 ]];
let n = 3;
document.write( "Original Matrix:<br>" );
printMat(mat, n);
sortMatRowAndColWise(mat, n);
document.write( "\nMatrix After Sorting:<br>" );
printMat(mat, n);
</script>
|
OutputOriginal Matrix:
4 1 3
9 6 8
5 2 7
Matrix After Sorting:
1 3 4
2 5 7
6 8 9
Time Complexity: O(n2log2n).
Auxiliary Space: O(1). Since no extra space has been taken.