Wednesday, July 3, 2024
HomeLanguagesJavaJava 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!

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments