Javascript Program for Sort the given matrix Last Updated : 12 Sep, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 last element of row 'i-1'.Examples: Input : mat[][] = { {5, 4, 7}, {1, 3, 8}, {2, 9, 6} }Output : 1 2 3 4 5 6 7 8 9Approach: Create a temp[] array of size n^2. Starting with the first row one by one copy the elements of the given matrix into temp[]. Sort temp[]. Now one by one copy the elements of temp[] back to the given matrix. JavaScript // JavaScript implementation to sort // the given matrix let SIZE = 10 // function to sort the given matrix function sortMat(mat, n) { // temporary matrix of size n^2 let temp = new Array(n * n); let k = 0; // copy the elements of matrix one by one // into temp[] for (let i = 0; i < n; i++) for (let j = 0; j < n; j++) temp[k++] = mat[i][j]; // sort temp[] temp.sort(); // copy the elements of temp[] one by one // in mat[][] k = 0; for (let i = 0; i < n; i++) for (let j = 0; j < n; j++) mat[i][j] = temp[k++]; } // function to print the given 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 program to test above let mat = [[5, 4, 7], [1, 3, 8], [2, 9, 6]]; let n = 3; console.log("Original Matrix: "); printMat(mat, n); sortMat(mat, n); console.log("Matrix After Sorting: "); printMat(mat, n); // This code is contributed by Manoj OutputOriginal Matrix: 5 4 7 1 3 8 2 9 6 Matrix After Sorting: 1 2 3 4 5 6 7 8 9 Complexity Analysis:Time Complexity: O(n2log2n). Auxiliary Space: O(n2).Please refer complete article on Sort the given matrix for more details! Comment More infoAdvertise with us Next Article Javascript Program to Print a given matrix in reverse spiral form K kartik Follow Improve Article Tags : JavaScript Similar Reads Javascript 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 9Output : Median is 5If 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 9Output: Median is 4 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 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 9Input : 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 2 3 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 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 Like