Java Program to Sort the matrix row-wise and column-wise
Last Updated :
18 Jan, 2022
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 9
Input : 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 20
Approach: Following are the steps:
- Sort each row of the matrix.
- Get transpose of the matrix.
- Again sort each row of the matrix.
- Again get transpose of the matrix.
Algorithm for getting transpose of the matrix:
for (int i = 0; i < n; i++) {
for (int j = i + 1; i < n; i++) {
int temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
Java
// Java implementation to sort the
// matrix row-wise and column-wise
import java.util.Arrays;
class GFG
{
static final int MAX_SIZE=10;
// function to sort each row of the matrix
static void sortByRow(int mat[][], int n)
{
for (int i = 0; i < n; i++)
// sorting row number 'i'
Arrays.sort(mat[i]);
}
// function to find transpose of the matrix
static void transpose(int mat[][], int n)
{
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
{
// swapping element at index (i, j)
// by element at index (j, i)
int temp=mat[i][j];
mat[i][j]=mat[j][i];
mat[j][i]=temp;
}
}
// function to sort the matrix row-wise
// and column-wise
static void sortMatRowAndColWise(int mat[][],int n)
{
// sort rows of mat[][]
sortByRow(mat, n);
// get transpose of mat[][]
transpose(mat, n);
// again sort rows of mat[][]
sortByRow(mat, n);
// again get transpose of mat[][]
transpose(mat, n);
}
// function to print the 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 code
public static void main (String[] args)
{
int mat[][] = { { 4, 1, 3 },
{ 9, 6, 8 },
{ 5, 2, 7 } };
int n = 3;
System.out.print("Original Matrix:
");
printMat(mat, n);
sortMatRowAndColWise(mat, n);
System.out.print("
Matrix After Sorting:
");
printMat(mat, n);
}
}
// This code is contributed by Anant Agarwal.
Output:
Original Matrix:
4 1 3
9 6 8
5 2 7
Matrix After Sorting:
1 3 4
2 5 7
6 8 9
Time Complexity: O(n2log2n).
Auxiliary Space: O(1).
Please refer complete article on Sort the matrix row-wise and column-wise for more details!
Similar Reads
Java Program to Print Matrix in Z form Given a square matrix of order n*n, we need to print elements of the matrix in Z form Examples: Input : mat[][] = {1, 2, 3, 4, 5, 6, 7, 8, 9} Output : 1 2 3 5 7 8 9 Input : mat[][] = {5, 19, 8, 7, 4, 1, 14, 8, 2, 20, 1, 9, 1, 2, 55, 4} Output: 5 19 8 7 14 20 1 2 55 4 Java // Java program to print a
2 min read
How to read a Matrix from user in Java? Given task is to read a matrix from the user. The size and number of elements of matrices are to be read from the keyboard. Java // Java program to read a matrix from user import java.util.Scanner; public class MatrixFromUser { // Function to read matrix public static void readMatrixByUser() { int m
2 min read
How to read a Matrix from user in Java? Given task is to read a matrix from the user. The size and number of elements of matrices are to be read from the keyboard. Java // Java program to read a matrix from user import java.util.Scanner; public class MatrixFromUser { // Function to read matrix public static void readMatrixByUser() { int m
2 min read
Different Ways of Array Sorting Techniques in Java with JUnit Accounting software /medical software / educational software etc. requires sorted data to be given for some analysis and also to prepare bar charts, pie charts, bar graphs, etc., In this tutorial let us see how to get sorted data in a java way. We need to know about the comparable interface and comp
6 min read
Different Ways of Array Sorting Techniques in Java with JUnit Accounting software /medical software / educational software etc. requires sorted data to be given for some analysis and also to prepare bar charts, pie charts, bar graphs, etc., In this tutorial let us see how to get sorted data in a java way. We need to know about the comparable interface and comp
6 min read
Java Program for Selection Sort The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from the unsorted part and putting it at the beginning.Algorithm for Selection SortImplementation of Selection Sort in Java is mentioned below:Step 1: Array arr with N sizeStep 2: Init
2 min read