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 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;
}
}
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 ("
Matrix After Sorting:")
printMat(mat, n)
|
Output:
Original 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). Please refer complete article on Sort the matrix row-wise and column-wise for more details!
Approach 2: Using NumPy
The NumPy code uses the numpy library to sort the matrix rows and transpose the matrix. The NumPy method looks considerably simple.
- np.sort() function is used to sort each row of the matrix and mat.transpose() is used to find the transpose of the matrix.
- The sortMatRowAndColWise function first sorts each row of the matrix using sortByRow function, then finds the transpose of the matrix and again sorts each row of the matrix and finds the transpose.
- The printMat function is used to print the matrix.
Note: Install numpy in python using the following command: pip install numpy
Below is the code for the above approach:
Python3
import numpy as np
def sortByRow(mat):
return np.sort(mat, axis = 1 )
def sortMatRowAndColWise(mat):
mat = sortByRow(mat)
mat = mat.transpose()
mat = sortByRow(mat)
mat = mat.transpose()
return mat
def printMat(mat):
print (mat)
mat = np.array([[ 4 , 1 , 3 ],
[ 9 , 6 , 8 ],
[ 5 , 2 , 7 ]])
print ( "Original Matrix:" )
printMat(mat)
mat = sortMatRowAndColWise(mat)
print ( "Matrix After Sorting:" )
printMat(mat)
|
Output:

Output
Time Complexity: O(n^2 log n), where n is the number of elements in the matrix.
Auxiliary Space: O(n^2), as it creates a copy of the transposed matrix.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
29 Mar, 2023
Like Article
Save Article