Java Program to Sort the elements of a Matrix

In the previous article, we have discussed Java Program to Rotate the Matrix 180 degree

In this article we are going to see how we can write a program to sort elements in a matrix in JAVA language.

Java Program to Sort the elements of a Matrix

A 3*3 Matrix is having 3 rows and 3 columns where this 3*3 represents the dimension of the matrix. Means there are 3*3 i.e. total 9 elements in a 3*3 Matrix.

Let’s understand it in more simpler way.

                   | A00   A01   A02 |
Matrix A =  | A10   A11   A12 |
                   | A20   A21   A22 | 3*3
  • Matrix A represents a 3*3 matrix.
  • A‘ represents the matrix element
  • Aij‘ represents the matrix element at it’s matrix position/index.
  • i‘ represents the row index
  • j‘ represents the column index
  • Means A00=Aij  where i=0 and j=0A01=aij where i=0 and j=1 and like this.
  • Here we have started row value from 0 and column value from 0.

Let’s see different ways to Sort the elements of a Matrix.

Method-1: Java Program to Sort the elements of a Matrix By Static Initialization of Array Elements

Approach:

  • Initialize and declare two arrays of size 3×3, one with elements.
  • Copy the elements of the matrix into a 1D array.
  • Sort the 1D array then insert the elements into the matrix.
  • Print the resultant array.

Program:

import java.io.*;
import java.util.*;
public class matrix{
    public static void main(String args[])
    {
        // Initializing the 3X3 matrix i.e. 2D array
        int arr[][] = {{19,25,32},{40,54,62},{70,20,60}}, res[][] = new int[3][3];
        int row, col ;
        
        System.out.print("The matrix elements are : ");
        printMatrix(arr);

        System.out.print("\nThe sorted matrix:");
        printMatrix(sortMatrix(arr));
    }

    // Method to sort the matrix elements
    static int[][] sortMatrix(int arr[][])
    {
        int temp[] = new int [3*3];
        int k = 0,row,col;

        // Copying the array elements into a 1D array
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
            {
                temp[k++]=arr[row][col];
            }
        
        // Sorting the 1D array
        Arrays.sort(temp);
        k=0;

        // Copying the elements from the sorted array into the 2D array
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
            {
                arr[row][col]=temp[k++];
            }
        return arr;
    }

    // Method to print the matrix
    static void printMatrix(int arr[][])
    {
        int row, col;
        // Loop to print the elements
        for(row=0;row<3;row++)
        {
            // Used for formatting
            System.out.print("\n");
            for(col=0;col<3;col++)
            {
                System.out.print(arr[row][col]+" ");
            }
        }
        System.out.print("\n");
    }

}
Output:

The matrix elements are : 
19 25 32 
40 54 62 
70 20 60

The sorted matrix:
19 20 25 
32 40 54 
60 62 70

Method-2: Java Program to Sort the elements of a Matrix By Dynamic Initialization of Array Elements

Approach:

  • Declare two arrays of size 3×3.
  • Ask the user for input of array elements and store them in the one array using two for loops.
  • Copy the elements of the matrix into a 1D array.
  • Sort the 1D array then insert the elements into the matrix.
  • Print the resultant array.

Program:

import java.io.*;
import java.util.*;
public class matrix{
    public static void main(String args[])
    {
        //Scanner class to take input
        Scanner scan = new Scanner(System.in);

        // Initializing the 3X3 matrix i.e. 2D array
        int arr[][] = new int[3][3], res[][] = new int[3][3];
        int row, col ;
        
        // Taking matrix input
        System.out.println("\nEnter matrix elements : ");
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
                arr[row][col] = scan.nextInt();
        
        System.out.print("The matrix elements are : ");
        printMatrix(arr);

        System.out.print("\nThe sorted matrix : ");
        printMatrix(sortMatrix(arr));
    }

    // Metrhod to sort the matrix elements
    static int[][] sortMatrix(int arr[][])
    {
        int temp[] = new int [3*3];
        int k = 0,row,col;

        // Copying the array elements into a 1D array
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
            {
                temp[k++]=arr[row][col];
            }
        
        // Sorting the 1D array
        Arrays.sort(temp);
        k=0;

        // Copying the elements from the sorted array into the 2D array
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
            {
                arr[row][col]=temp[k++];
            }
        return arr;
    }

    // Method to print the matrix
    static void printMatrix(int arr[][])
    {
        int row, col;
        // Loop to print the elements
        for(row=0;row<3;row++)
        {
            // Used for formatting
            System.out.print("\n");
            for(col=0;col<3;col++)
            {
                System.out.print(arr[row][col]+" ");
            }
        }
        System.out.print("\n");
    }

}
Output:

Enter matrix elements : 9 8 3 4 5 6 7 2 1
The matrix elements are : 
9 8 3 
4 5 6 
7 2 1

The sorted matrix : 
1 2 3 
4 5 6 
7 8 9

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: