# Sort Matrix Column-wise in Java

Write a program to allow the user to enter integers into a two-dimensional array of size M × N.

The size of the rows and columns is also entered by the user.

Display the original matrix.

Now sort the integers column-wise in ascending order in the matrix, and display the resultant matrix.

## Sorting in the matrix itself

``````import java.io.*;
class SortColumns{
public static void main(String args[])throws IOException{
System.out.print("Number of rows: ");
System.out.print("Number of columns: ");
int a[][] = new int[m][n];
System.out.println("Enter array elements:");
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
}
}
System.out.println("Original matrix:");
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
System.out.print(a[i][j] + "\t");
}
System.out.println();
}
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
for(int k = 0; k < m - 1 - j; k++){
if(a[k][i] > a[k + 1][i]){
int temp = a[k][i];
a[k][i] = a[k + 1][i];
a[k + 1][i] = temp;
}
}
}
}
System.out.println("Sorted matrix:");
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
System.out.print(a[i][j] + "\t");
}
System.out.println();
}
}
}``````

## Sorting in a separate array

``````import java.io.*;
class SortColumns{
public static void main(String args[])throws IOException{
System.out.print("Number of rows: ");
System.out.print("Number of columns: ");
if(m < 3 || m > 9 || n < 3 || n > 9){
System.out.println("Size out of range!");
return;
}
int a[][] = new int[m][n];
System.out.println("Enter array elements:");
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
}
}
System.out.println("Original matrix:");
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
System.out.print(a[i][j] + "\t");
}
System.out.println();
}
for(int i = 0; i < n; i++){
int t[] = new int[m];
for(int j = 0; j < m; j++){
t[j] = a[j][i];
}
for(int p = 0; p < t.length; p++){
for(int q = 0; q < t.length - 1 - p; q++){
if(t[q] > t [q + 1]){
int temp = t[q];
t[q] = t[q + 1];
t[q + 1] = temp;
}
}
}
for(int j = 0; j < m; j++){
a[j][i] = t[j];
}
}
System.out.println("Sorted matrix:");
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
System.out.print(a[i][j] + "\t");
}
System.out.println();
}
}
}``````

## 7 thoughts on “Sort Matrix Column-wise in Java”

1. sayan rana on said:

1)Class Binary contains an array of n integers (n<=100) that are already arranged in ascending order. The
subscripts of the array elements vary from 0 to n-1. The data members and member functions of class
Binary are given below:
Class name : Binary
Data members
A[] : integer array of 100 elements
n : size of array
l location of the lower bound
u : location of the upper bound
Member functions:
Binary(int nn) : constructor to initialize the size n to nn and the
other instance variables.
void readdata() : to fill the elements of the array in ascending order.
intbinary_search(int v) : returns the location of the value(v) to be
searched in the list by binary search method
using the recursive technique. The function
returns -1 if the number is not present in the
given list.
a) Specify the class Binary giving details of the constructor, void readdata() and
int binary_search(int). Write main() method
b) State the base case in the recursive technique binary_search().
c) What are the drawbacks of using the recursive technique.
2) A transpose of an array is obtained by interchanging the elements of rows and columns. A class
Transarray contains a two dimensional integer array of order [ m x n]. The maximum value possible for
both ‘m’ and ‘n’ is 20. Design a class Transarray to find the transpose of a given matrix. The details of the
members of the class are given below:
Class name : Trasarray
Data members
arr[][] : stores the matrix elements
m : integer to store the number of rows.
n : integer to store the number of columns.
Member functions
Transarray() : default constructor.
Transarray(intmm,intnn) : to inititalize the size of the matrix, m=mm,n=nn.
voidfillarray() : to enter elements into the matrix.
void transpose(Transarray A) : to find the transpose of a given matrix.
voiddisaparray() : displays the array in a matrix form.
Specify the class Transarray giving details of the constructors, void fillarray(), void transpose(Transarray)
and void disparray(). write the main function also.
[

• admin on said:

Here is the program that solves binary search problem using recursion.

2. sayan rana on said:

Sample Input & Output for the above program I sent is given:
M = 4
N = 3
Enter elements of matrix:
11
-2
3
5
16
7
9
0
4
3
1
8

Original Matrix:
11 -2 3
5 16 7
9 0 4
3 1 8
Matrix after sorting columns:
3 -2 3
5 0 4
9 1 7
11 16 8
How to solve the program by creating a single dimensional array and passing each elements in the column in a single dimensional array?

• admin on said:

Refer to the second program in this post for sorting columns in a matrix using a separate one-dimensional array.

3. sayan rana on said:

Can you show me the way how to write the program which I had mentioned?

• admin on said:

4. sayan rana on said:

Sir,a program is given which states that
Write a program to allow the user to enter integers into a two-dimensional array of size M × N.

The size of the rows and columns is also entered by the user.(M & N must be greater than 2 and less than 10)

Display the original matrix.

Now sort the integers column-wise in ascending order in the matrix, and display the resultant matrix.
How to solve the program by creating a single dimensional array and passing each elements in the column in a single dimensional array?

This site uses Akismet to reduce spam. Learn how your comment data is processed.