ISC Practicals and Theory - Row-wise and Column-wise Sorting
Updated 21 Oct 2026
A matrix can be sorted in two ways: every row on its own, or every column on its own. This program, written in the main method alone, reads an m x n matrix, makes two separate copies of it, sorts every row of one copy and every column of the other in ascending order with bubble sort, and prints the original and both results.
Definition
Row-wise sorting arranges the elements of each row in ascending order, one row at a time; the rows stay in the same order, and elements never move from one row to another. Column-wise sorting arranges the elements of each column in ascending order from top to bottom, one column at a time. Ascending means each element is smaller than or equal to the one after it.
For the matrix with rows 9 2 7 and 1 8 6:
- sorted row-wise:
2 7 9and1 6 8 - sorted column-wise:
1 2 6and9 8 7
The two results are independent, so each sort is done on its own copy of the original. If the column sort were done on the already row-sorted matrix, the answer would be different. The numbers of rows and columns must each be from 2 to 10, otherwise the input is invalid.
Algorithm
- Read the number of rows
mand the number of columnsn. If either is not from 2 to 10, print an error message and stop. - Read the elements of the matrix
arow by row. The originalais never changed after the input. - Make two separate copies of
a:rsfor the row sort andcsfor the column sort. - Row sort on
rs: take one rowiat a time and apply bubble sort to it. In each passp, compare neighboursrs[i][q]andrs[i][q + 1]and swap them if the left one is larger. - Column sort on
cs: take one columnjat a time and apply bubble sort down the column, comparingcs[q][j]andcs[q + 1][j]. - Print the original matrix, the row-sorted matrix and the column-sorted matrix.
Program
Everything is written inside main: the input, the copying, one triple loop for the rows, one triple loop for the columns, and three printing loops.
import java.util.Scanner;
public class MatrixSort
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows m (2 to 10): ");
int m = sc.nextInt(); // number of rows of the matrix
if (m < 2 || m > 10) // rows out of range
{
System.out.println("Invalid number of rows");
return;
}
System.out.print("Enter the number of columns n (2 to 10): ");
int n = sc.nextInt(); // number of columns of the matrix
if (n < 2 || n > 10) // columns out of range
{
System.out.println("Invalid number of columns");
return;
}
int a[][] = new int[m][n]; // stores the original matrix (never changed after input)
System.out.println("Enter the elements of the matrix:");
for (int i = 0; i < m; i++) // i runs over the rows
{
for (int j = 0; j < n; j++) // j runs over the columns
{
a[i][j] = sc.nextInt();
}
}
int rs[][] = new int[m][n]; // copy of a[][] in which every row is sorted
int cs[][] = new int[m][n]; // copy of a[][] in which every column is sorted
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
rs[i][j] = a[i][j];
cs[i][j] = a[i][j];
}
}
for (int i = 0; i < m; i++) // take one row at a time
{
for (int p = 0; p < n - 1; p++) // number of passes
{
for (int q = 0; q < n - 1 - p; q++) // compare neighbours in the row
{
if (rs[i][q] > rs[i][q + 1]) // larger one moves right
{
int t = rs[i][q];
rs[i][q] = rs[i][q + 1];
rs[i][q + 1] = t;
}
}
}
}
for (int j = 0; j < n; j++) // take one column at a time
{
for (int p = 0; p < m - 1; p++) // number of passes
{
for (int q = 0; q < m - 1 - p; q++) // compare neighbours in the column
{
if (cs[q][j] > cs[q + 1][j]) // larger one moves down
{
int t = cs[q][j];
cs[q][j] = cs[q + 1][j];
cs[q + 1][j] = t;
}
}
}
}
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"); // tab between elements
}
System.out.println(); // new line after each row
}
System.out.println("Rows sorted in ascending order:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(rs[i][j] + "\t");
}
System.out.println();
}
System.out.println("Columns sorted in ascending order:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(cs[i][j] + "\t");
}
System.out.println();
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the numbers typed by the user |
m | int | Number of rows of the matrix |
n | int | Number of columns of the matrix |
a | int[][] | The original matrix, never changed after the input |
rs | int[][] | Copy of a in which every row is sorted |
cs | int[][] | Copy of a in which every column is sorted |
i | int | Loop counter over the rows |
j | int | Loop counter over the columns |
p | int | Pass number of the bubble sort |
q | int | Index of the pair of neighbours being compared |
t | int | Temporary variable used to swap two elements |
Sample input and output
Sample 1
Enter the number of rows m (2 to 10): 3
Enter the number of columns n (2 to 10): 4
Enter the elements of the matrix:
9 2 7 5
1 8 6 3
4 0 -2 5
Original matrix:
9 2 7 5
1 8 6 3
4 0 -2 5
Rows sorted in ascending order:
2 5 7 9
1 3 6 8
-2 0 4 5
Columns sorted in ascending order:
1 0 -2 3
4 2 6 5
9 8 7 5 Sample 2
Enter the number of rows m (2 to 10): 2
Enter the number of columns n (2 to 10): 3
Enter the elements of the matrix:
5 5 5
5 5 5
Original matrix:
5 5 5
5 5 5
Rows sorted in ascending order:
5 5 5
5 5 5
Columns sorted in ascending order:
5 5 5
5 5 5 Sample 3
Enter the number of rows m (2 to 10): 3
Enter the number of columns n (2 to 10): 12
Invalid number of columnsSample 4
Enter the number of rows m (2 to 10): 1
Invalid number of rowsDry run
Take the first sample, $m = 3$ and $n = 4$. First the row 9 2 7 5 (row 0 of rs) is sorted by the row-sorting loops.
| p | q | Comparison | Action | Row after the step |
|---|---|---|---|---|
| 0 | 0 | 9 > 2 | swap | 2 9 7 5 |
| 0 | 1 | 9 > 7 | swap | 2 7 9 5 |
| 0 | 2 | 9 > 5 | swap | 2 7 5 9 |
| 1 | 0 | 2 > 7 | no swap | 2 7 5 9 |
| 1 | 1 | 7 > 5 | swap | 2 5 7 9 |
| 2 | 0 | 2 > 5 | no swap | 2 5 7 9 |
The sorted row is 2 5 7 9, the first row of the row-sorted output. Now column 3 of cs, which holds 5, 3, 5 from top to bottom, is sorted by the column-sorting loops.
| p | q | Comparison | Action | Column after the step |
|---|---|---|---|---|
| 0 | 0 | 5 > 3 | swap | 3 5 5 |
| 0 | 1 | 5 > 5 | no swap | 3 5 5 |
| 1 | 0 | 3 > 5 | no swap | 3 5 5 |
The last column of the column-sorted output is therefore 3 5 5, the same as the first sample run.
Exam tip: Sort a copy for each result. If you sort the original, the second sort starts from an already changed matrix and the original can no longer be printed.
Variations
- Sort the whole matrix "Sort all the elements of a matrix in ascending order and display the sorted matrix."
- Descending order "Sort every row of a matrix in descending order and display it."
- Row sum and column sum "Display the sum of each row and each column of the matrix."