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
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
Javascript Program to Inplace rotate square matrix by 90 degrees | Set 1 Given a square matrix, turn it by 90 degrees in anti-clockwise direction without using any extra space.Examples : Input:Matrix: 1 2 3 4 5 6 7 8 9Output: 3 6 9 2 5 8 1 4 7 The given matrix is rotated by 90 degree in anti-clockwise direction.Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 4 8 12
5 min read
Javascript Program to Print a given matrix in reverse spiral form Given a 2D array, print it in reverse spiral form. We have already discussed Print a given matrix in spiral form. This article discusses how to do the reverse printing. See the following examples. Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16Output: 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1Input: 1 2 3
3 min read
JavaScript Program to Find Kth Smallest/Largest Element in a Sorted Matrix Given an n x n matrix, where every row and column is sorted in non-decreasing order. Find the kth smallest/largest element in the given 2D array. Examples: Input:k = 3 and array = 10, 20, 30, 40 15, 25, 35, 45 24, 29, 37, 48 32, 33, 39, 50 Output: 20 40Explanation: The 3rd smallest element is 20 The
3 min read
Javascript Program For Sorting An Array Of 0s, 1s and 2s Given an array A[] consisting 0s, 1s and 2s. The task is to write a function that sorts the given array. The functions should put all 0s first, then all 1s and all 2s in last.Examples:Input: {0, 1, 2, 0, 1, 2}Output: {0, 0, 1, 1, 2, 2}Input: {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1}Output: {0, 0, 0, 0, 0
5 min read
JavaScript Program to Find the Rank of a Matrix Given a matrix of size M x N, your task is to find the rank of a matrix using JavaScript.Example:Input: mat[][] = [[10, 20, 10], [20, 40, 20], [30, 50, 0]]Output: Rank is 2Explanation: Ist and 2nd rows are linearly dependent. But Ist and 3rd or 2nd and 3rd are independent. Input: mat[][] = [[10, 20,
5 min read