Wednesday, September 25, 2024
Google search engine
HomeData Modelling & AIJava Program for Sort the given matrix

Java 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 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

Ā 

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.
Ā 

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.


Output:Ā Ā 

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).
Ā 

Please refer complete article on Sort the given matrix for more details!

Feeling lost in the world of random DSA topics, wasting time without progress? Itā€™s time for a change! Join our DSA course, where weā€™ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?