Can anyone please teach me how to sort a matrix row and column wise ?
Please also share the code of the same.
Sorting a matrix row and column wise
@Tiwary725 hi,you can sort matrix rowwise and columnwise using following 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.
Pseudo code for sorting each row of matrix using C++ STL sort():
for (int i = 0 ; i < n; i++)
sort(mat[i], mat[i] + n);
Pseudo code 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;
}
}
Hope you get it
Do we need to sort and transpose twice ?
For soeting a given matrix row and column wise, can we sort it column wise at first and then row wise?
@Tiwary725 yes you can do so it will give same ans ,above logic with four steps is for same and yes we have to take transponse two times after sorting each time.