Write a program to declare a matrix a[][] of order (m × n) where ‘m’ is the number of rows and ‘n’ is the number of columns such that the values of both ‘m’ and ‘n’ must be greater than 2 and less than 10. Allow the user to input integers into this matrix. Perform the following tasks on the matrix:

- Display the original matrix.
- Sort each row of the matrix in ascending order using any standard sorting technique.
- Display the changed matrix after sorting each row.

Test your program for the following data and some random data:

**Example 1:**

INPUT:

m = 4

n = 3

Enter elements of matrix:

11 -2 3 5 16 7 9 0 4 3 1 8

OUTPUT:

Original Matrix:

11 -2 3 5 16 7 9 0 4 3 1 8

Matrix after sorting rows:

-2 3 11 5 7 16 0 4 9 1 3 8

**Example 2:**

INPUT:

m = 3

n = 3

Enter elements of matrix:

22 5 19 7 36 12 9 13 6

OUTPUT:

Original matrix:

22 5 19 7 36 12 9 13 6

Matrix after sorting rows:

5 19 22 7 12 36 6 9 13

**Example 3:**

INPUT:

m = 11

n = 5

OUTPUT:

Matrix size out of range.

**Program:**

```
import java.io.*;
class MatrixSort{
public static void main(String args[])
throws IOException{
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
int m = 0;
int n = 0;
int a[][];
int i = 0;
int j = 0;
int temp[];
int k = 0;
System.out.print("M = ");
m = Integer.parseInt(br.readLine());
System.out.print("N = ");
n = Integer.parseInt(br.readLine());
if(m <= 2 || m >= 10 || n <= 2 || n >= 10){
System.out.println("Matrix size out of range.");
return;
}
a = new int[m][n];
temp = new int[m * n];
System.out.println("Enter elements of matrix:");
for(i = 0; i < m; i++){
for(j = 0; j < n; j++){
a[i][j] = Integer.parseInt(br.readLine());
temp[k++] = a[i][j];
}
}
System.out.println("Original Matrix:");
for(i = 0; i < m; i++){
for(j = 0; j < n; j++)
System.out.print(a[i][j] + "\t");
System.out.println();
}
for(i = 0; i < temp.length; i++){
for(j = 0; j < temp.length - 1 - i; j++){
if(temp[j] > temp[j + 1]){
int t = temp[j];
temp[j] = temp[j + 1];
temp[j + 1] = t;
}
}
}
k = 0;
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
a[i][j] = temp[k++];
System.out.println("Matrix after sorting rows:");
for(i = 0; i < m; i++){
for(j = 0; j < n; j++)
System.out.print(a[i][j] + "\t");
System.out.println();
}
}
}
```

## 10 replies on “Matrix Sorting Java Program ISC 2018 Practical”

Sir,a program is given which states that:

Write a program to create a matrix a[][] of order (m × n) where ‘m’ is the number of rows and ‘n’ is the number of columns such that the values of both ‘m’ and ‘n’ must be greater than 2 and less than 10 .Now fill each row of the matrix uniformly by 1 or 0 alternately.

Sample Input:

M=3

N=3

Resultant matrix:

1 1 1

0 0 0

1 1 1

Sir,a program is given which states that:

Write a program to declare a matrix a[][] of order (m × n) where ‘m’ is the number of rows and ‘n’ is the number of columns such that the values of both ‘m’ and ‘n’ must be greater than 2 and less than 10. Allow the user to input integers into this matrix. Perform the following tasks on the matrix:

Display the original matrix.

Sort each row of the matrix in ascending order using any standard sorting technique.

Display the changed matrix after sorting each row.

Test your program for the following data and some random data:

Example 1:

INPUT:

m = 4

n = 3

Enter elements of matrix:

11 -2 3

5 16 7

9 0 4

3 1 8

OUTPUT:

Original Matrix:

11 -2 3

5 16 7

9 0 4

3 1 8

How to solve the program by creating a single dimensional array and passing each elements in the column in a single dimensional array?

Copy the elements from each row to the 1-D array and sort the elements.

Then place those values back to the corresponding row.

Sir,How to arrange each row of m x n matrix in descending order using selection sort technique?Sir,how to write the code in Java?

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 rows:

11 3 -2

16 7 5

9 4 0

8 3 1

Sir,I am unable to sort each row using selection sort technique.Can you please help me with the code in Java?

Here is the link to your program. However, to sort in descending order, just change the > sign to < sign while sorting.

Sir,can you show me how to write the above program mentioned in Java?Sample Input and Output is given to you

Okay, I will soon write the program and will post on the website.

Thank you,sir for helping me in the previous 2 programs.

Sir,How to arrange each row of m x n matrix in descending order using selection sort technique?Sir,how to write the code in Java?

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 rows:

11 3 -2

16 7 5

9 4 0

8 3 1

I will soon be posting a program to sort a matrix row-wise.

Here is the link to your program. However, to sort in descending order, just change the > sign to < sign while sorting.