C++ Program to Sort the matrix row-wise and column-wise
Last Updated :
18 Jan, 2022
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;
}
}
C++
// C++ implementation to sort the matrix row-wise
// and column-wise
#include <bits/stdc++.h>
using namespace std;
#define MAX_SIZE 10
// function to sort each row of the matrix
void sortByRow(int mat[MAX_SIZE][MAX_SIZE], int n)
{
for (int i = 0; i < n; i++)
// sorting row number 'i'
sort(mat[i], mat[i] + n);
}
// function to find transpose of the matrix
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++)
// swapping element at index (i, j)
// by element at index (j, i)
swap(mat[i][j], mat[j][i]);
}
// function to sort the matrix row-wise
// and column-wise
void sortMatRowAndColWise(int mat[MAX_SIZE][MAX_SIZE],
int n)
{
// sort rows of mat[][]
sortByRow(mat, n);
// get transpose of mat[][]
transpose(mat, n);
// again sort rows of mat[][]
sortByRow(mat, n);
// again get transpose of mat[][]
transpose(mat, n);
}
// function to print the matrix
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;
}
}
// Driver program to test above
int main()
{
int mat[MAX_SIZE][MAX_SIZE] = { { 4, 1, 3 },
{ 9, 6, 8 },
{ 5, 2, 7 } };
int n = 3;
cout << "Original Matrix:
";
printMat(mat, n);
sortMatRowAndColWise(mat, n);
cout << "
Matrix After Sorting:
";
printMat(mat, n);
return 0;
}
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!
Similar Reads
C++ Program to Find median in row wise sorted matrix We are given a row-wise sorted matrix of size r*c, we need to find the median of the matrix given. It is assumed that r*c is always odd.Examples:Â Input : 1 3 5 2 6 9 3 6 9 Output : Median is 5 If we put all the values in a sorted array A[] = 1 2 3 3 5 6 6 9 9) Input: 1 3 4 2 5 6 7 8 9 Output: Media
4 min read
C++ Program to Sort the given matrix Given a n x n matrix. The problem is to sort the given matrix in strict order. Here strict order means that matrix is sorted in a way such that all elements in a row are sorted in increasing order and for row âiâ, where 1 Examples:Â Â Input : mat[][] = { {5, 4, 7}, {1, 3, 8}, {2, 9, 6} } Output : 1 2
2 min read
Sort the matrix column-wise Given a matrix mat[][] of dimensions N * M, the task is to sort each column of a matrix in ascending order and print the updated matrix. Examples: Input: mat[][] = { {1, 6, 10}, {8, 5, 9}, {9, 4, 15}, {7, 3, 60} }Output:1 3 97 4 108 5 159 6 60Explanation:The input matrix is sorted in a column wise m
8 min read
C++ Program For Row Wise Sorting in 2D Array Given a 2D array, sort each row of this array and print the result.Examples: Input: 77 11 22 3 11 89 1 12 32 11 56 7 11 22 44 33 Output: 3 11 22 77 1 11 12 89 7 11 32 56 11 22 33 44 Input: 8 6 4 5 3 5 2 1 9 7 4 2 7 8 9 5 Output: 4 5 6 8 1 2 3 5 2 4 7 9 5 7 8 9 Method 1 (Using Bubble Sort): Start ite
3 min read
C++ program to Convert a Matrix to Sparse Matrix Given a matrix with most of its elements as 0, convert this matrix to sparse matrix in C++Examples: Input: Matrix: 0 1 1 1 2 2 2 1 3 3 2 5 4 3 4 Output: Sparse Matrix: 0 1 0 0 0 0 2 0 0 3 0 0 0 0 5 0 0 0 0 4 Explanation: Here the Sparse matrix is represented in the form Row Column Value Hence the ro
3 min read