Enter elements of matrix:
9
1
5
3
7
4
8
6
2
Original Matrix:
9 1 5
3 7 4
8 6 2
Matrix after sorting rows:
1 5 9
3 4 7
2 6 8
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 a[][];
int i = 0;
int j = 0;
int temp[];
int k = 0;
a = new int[3][3];
temp = new int[3 * 3];
System.out.println("Enter elements of matrix:");
for(i = 0; i < 3; i++){
for(j = 0; j < 3; j++){
a[i][j] = Integer.parseInt(br.readLine());
temp[k++] = a[i][j];
}
}
System.out.println("Original Matrix:");
for(i = 0; i < 3; i++){
for(j = 0; j < 3; j++)
System.out.print(a[i][j] + "\t");
System.out.println();
}
int z=0;
int min=0;
while(z<3*3)
{
for(int o=0;o<3*3;o=o+3)
{
for(i = 0; i < 2; i++){
min=i+o;
for(j = i+1; j < 3; j++)
{
if(temp[j+o] < temp[min])
min=j+o;
}
int t = temp[i+o];
temp[i+o] = temp[min];
temp[min] = t;
}
k = 0;
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
a[i][j] = temp[k++];}
z++;}
System.out.println("Matrix after sorting rows:");
for(i = 0; i < 3; i++){
for(j = 0; j < 3; j++)
System.out.print(a[i][j] + "\t");
System.out.println();
}
}
}
9
1
5
3
7
4
8
6
2
Original Matrix:
9 1 5
3 7 4
8 6 2
Matrix after sorting rows:
1 5 9
3 4 7
2 6 8
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 a[][];
int i = 0;
int j = 0;
int temp[];
int k = 0;
a = new int[3][3];
temp = new int[3 * 3];
System.out.println("Enter elements of matrix:");
for(i = 0; i < 3; i++){
for(j = 0; j < 3; j++){
a[i][j] = Integer.parseInt(br.readLine());
temp[k++] = a[i][j];
}
}
System.out.println("Original Matrix:");
for(i = 0; i < 3; i++){
for(j = 0; j < 3; j++)
System.out.print(a[i][j] + "\t");
System.out.println();
}
int z=0;
int min=0;
while(z<3*3)
{
for(int o=0;o<3*3;o=o+3)
{
for(i = 0; i < 2; i++){
min=i+o;
for(j = i+1; j < 3; j++)
{
if(temp[j+o] < temp[min])
min=j+o;
}
int t = temp[i+o];
temp[i+o] = temp[min];
temp[min] = t;
}
k = 0;
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
a[i][j] = temp[k++];}
z++;}
System.out.println("Matrix after sorting rows:");
for(i = 0; i < 3; i++){
for(j = 0; j < 3; j++)
System.out.print(a[i][j] + "\t");
System.out.println();
}
}
}