Javascript Program to Sort the matrix row-wise and column-wise
Last Updated :
12 Sep, 2024
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:
The 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;
}
}
JavaScript
// Javascript implementation to sort the
// matrix row-wise and column-wise
let MAX_SIZE = 10;
// function to sort each row of the matrix
function sortByRow(mat, n) {
for (let i = 0; i < n; i++)
// sorting row number 'i'
mat[i].sort(function (a, b) { return a - b; });
}
// function to find transpose of the matrix
function transpose(mat, n) {
for (let i = 0; i < n; i++)
for (let j = i + 1; j < n; j++) {
// swapping element at index (i, j)
// by element at index (j, i)
let temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
// function to sort the matrix row-wise
// and column-wise
function sortMatRowAndColWise(mat, 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
function printMat(mat, n) {
for (let i = 0; i < n; i++) {
let output = '';
for (let j = 0; j < n; j++)
output += mat[i][j] + " ";
console.log(output);
}
}
// Driver code
let mat = [[4, 1, 3],
[9, 6, 8],
[5, 2, 7]];
let n = 3;
console.log("Original Matrix:<br>");
printMat(mat, n);
sortMatRowAndColWise(mat, n);
console.log("Matrix After Sorting:");
printMat(mat, n);
// This code is contributed by avanitrachhadiya2155
OutputOriginal Matrix:<br>
4 1 3
9 6 8
5 2 7
Matrix After Sorting:
1 3 4
2 5 7
6 8 9
Complexity Analysis:
- 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
Sort the matrix row-wise and column-wise 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 1
9 min read
Javascript Program for 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 <= i <= n-1, first element of row 'i' is greater than or equal to the la
2 min read
C Program to sort rows of the Matrix Given a matrix arr[][] of dimensions N * M, the task is to sort the matrix such that each row is sorted and the first element of each row is greater than or equal to the last element of the previous row. Examples: Input: N = 3, M = 3, arr[][] = {{7, 8, 9}, {5, 6, 4}, {3, 1, 2}}Output:1 2 34 5 67 8 9
3 min read
Sort the Matrix based on the given column number Given a Matrix of size M * N (0-indexed) and an integer K, the matrix contains distinct integers only, the task is to sort the Matrix (i.e., the rows of the matrix) by their Values in the Kth column, from Maximum to Minimum. Return the matrix after sorting it. Examples: Input: Matrix = [[10, 6, 9, 1
12 min read
Print all elements in sorted order from row and column wise sorted matrix Given an n x n matrix, where every row and column is sorted in non-decreasing order. Print all elements of the matrix in sorted order. Example: Input: mat[][] = { {10, 20, 30, 40}, {15, 25, 35, 45}, {27, 29, 37, 48}, {32, 33, 39, 50}, };Output: 10 15 20 25 27 29 30 32 33 35 37 39 40 45 48 50 Recomme
15+ min read