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 the 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, the 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 9
Brute Force Using Extra O(n*m) Space:
The Approach:
here in this approach we store the all the elment in a vector then sort it we need to fill that matrix again.
C++
#include <iostream> #include<bits/stdc++.h> using namespace std; int main() { vector<vector< int >>v{{5, 4, 7},{1, 3, 8},{2, 9, 6}}; int n=v.size(); vector< int >x; for ( int i=0;i<n;i++){ for ( int j=0;j<n;j++){ x.push_back(v[i][j]); } } sort(x.begin(),x.end()); int k=0; for ( int i=0;i<n;i++){ for ( int j=0;j<n;j++){ v[i][j]=x[k++]; } } cout<< "Sorted Matrix Will be:" <<endl; for ( auto it:v){ for ( auto vt:it){ cout<<vt<< " " ; }cout<<endl; } return 0; } |
Javascript
// JavaScript program for the above approach let v = [[5,4,7], [1,3,8], [2,9,6]]; let n = v.length; let x = []; for (let i = 0; i<n; i++){ for (let j = 0; j<n; j++){ x.push(v[i][j]); } } x.sort(); let k = 0; for (let i = 0; i<n; i++){ for (let j = 0; j<n; j++){ v[i][j] = x[k++]; } } document.write( "Sorted Matrix will be : <br>" ); for (let i = 0; i<n; i++){ for (let j = 0; j<n; j++){ document.write(v[i][j] + " " ); } document.write( "<br>" ); } // this code is contributed by Yash Agarwal(yashagarwal2852002) |
Sorted Matrix Will be: 1 2 3 4 5 6 7 8 9
Time Complexity: O(n*n),For traversing
Auxiliary Space: O(n*n),For vector.
Approach: 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.
Below is the implementation of the above approach:
C++
// C++ implementation to sort the given matrix #include <bits/stdc++.h> using namespace std; #define SIZE 10 // function to sort the given matrix void sortMat( int mat[SIZE][SIZE], int n) { // temporary matrix of size n^2 int temp[n * n]; int k = 0; // copy the elements of matrix one by one // into temp[] for ( int i = 0; i < n; i++) for ( int j = 0; j < n; j++) temp[k++] = mat[i][j]; // sort temp[] sort(temp, temp + k); // copy the elements of temp[] one by one // in mat[][] k = 0; for ( int i = 0; i < n; i++) for ( int j = 0; j < n; j++) mat[i][j] = temp[k++]; } // function to print the given matrix void printMat( int mat[SIZE][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[SIZE][SIZE] = { { 5, 4, 7 }, { 1, 3, 8 }, { 2, 9, 6 } }; int n = 3; cout << "Original Matrix:\n" ; printMat(mat, n); sortMat(mat, n); cout << "\nMatrix After Sorting:\n" ; printMat(mat, n); return 0; } |
Java
// Java implementation to // sort the given matrix import java.io.*; import java.util.*; class GFG { static int SIZE = 10 ; // function to sort the given matrix static void sortMat( int mat[][], int n) { // temporary matrix of size n^2 int temp[] = new int [n * n]; int k = 0 ; // copy the elements of matrix // one by one into temp[] for ( int i = 0 ; i < n; i++) for ( int j = 0 ; j < n; j++) temp[k++] = mat[i][j]; // sort temp[] Arrays.sort(temp); // copy the elements of temp[] // one by one in mat[][] k = 0 ; for ( int i = 0 ; i < n; i++) for ( int j = 0 ; j < n; j++) mat[i][j] = temp[k++]; } // function to print the given matrix static void printMat( int mat[][], int n) { for ( int i = 0 ; i < n; i++) { for ( int j = 0 ; j < n; j++) System.out.print( mat[i][j] + " " ); System.out.println(); } } // Driver program to test above public static void main(String args[]) { int mat[][] = { { 5 , 4 , 7 }, { 1 , 3 , 8 }, { 2 , 9 , 6 } }; int n = 3 ; System.out.println( "Original Matrix:" ); printMat(mat, n); sortMat(mat, n); System.out.println( "Matrix After Sorting:" ); printMat(mat, n); } } // This code is contributed by Nikita Tiwari. |
Python3
# Python3 implementation to sort # the given matrix SIZE = 10 # Function to sort the given matrix def sortMat(mat, n) : # Temporary matrix of size n^2 temp = [ 0 ] * (n * n) k = 0 # Copy the elements of matrix # one by one into temp[] for i in range ( 0 , n) : for j in range ( 0 , n) : temp[k] = mat[i][j] k + = 1 # sort temp[] temp.sort() # copy the elements of temp[] # one by one in mat[][] k = 0 for i in range ( 0 , n) : for j in range ( 0 , n) : mat[i][j] = temp[k] k + = 1 # Function to print the given matrix def printMat(mat, n) : for i in range ( 0 , n) : for j in range ( 0 , n ) : print (mat[i][j] , end = " " ) print () # Driver program to test above mat = [ [ 5 , 4 , 7 ], [ 1 , 3 , 8 ], [ 2 , 9 , 6 ] ] n = 3 print ( "Original Matrix:" ) printMat(mat, n) sortMat(mat, n) print ( "\nMatrix After Sorting:" ) printMat(mat, n) # This code is contributed by Nikita Tiwari. |
C#
// C# implementation to // sort the given matrix using System; class GFG { static int SIZE = 10; // function to sort the given matrix static void sortMat( int [, ] mat, int n) { // temporary matrix of size n^2 int [] temp = new int [n * n]; int k = 0; // copy the elements of matrix // one by one into temp[] for ( int i = 0; i < n; i++) for ( int j = 0; j < n; j++) temp[k++] = mat[i, j]; // sort temp[] Array.Sort(temp); // copy the elements of temp[] // one by one in mat[][] k = 0; for ( int i = 0; i < n; i++) for ( int j = 0; j < n; j++) mat[i, j] = temp[k++]; } // function to print the given matrix static void printMat( int [, ] mat, int n) { for ( int i = 0; i < n; i++) { for ( int j = 0; j < n; j++) Console.Write(mat[i, j] + " " ); Console.WriteLine(); } } // Driver code public static void Main() { int [, ] mat = { { 5, 4, 7 }, { 1, 3, 8 }, { 2, 9, 6 } }; int n = 3; Console.WriteLine( "Original Matrix:" ); printMat(mat, n); sortMat(mat, n); Console.WriteLine( "Matrix After Sorting:" ); printMat(mat, n); } } // This code is contributed by Sam007 |
Javascript
<script> // 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++) { for (let j = 0; j < n; j++) document.write( mat[i][j] + " " ); document.write( "<br>" ); } } // Driver program to test above let mat = [ [ 5, 4, 7 ], [ 1, 3, 8 ], [ 2, 9, 6 ] ]; let n = 3; document.write( "Original Matrix: " + "<br>" ); printMat(mat, n); sortMat(mat, n); document.write( "<br>" ); document.write( "\nMatrix After Sorting: " + "<br>" ); printMat(mat, n); // This code is contributed by Manoj </script> |
Original Matrix: 5 4 7 1 3 8 2 9 6 Matrix After Sorting: 1 2 3 4 5 6 7 8 9
Time Complexity: O(n2log2n).
Auxiliary Space: O(n2), since n * n extra space has been taken.
Please Login to comment...