-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMatColSortAcend.java
More file actions
67 lines (59 loc) · 1.95 KB
/
MatColSortAcend.java
File metadata and controls
67 lines (59 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package MatrixProb;
import java.util.Scanner;
public class MatColSortAcend {
private int row, col;
private int[][] arr;
// Constructor to initialize the matrix
public MatColSortAcend(int row, int col) {
this.row = row;
this.col = col;
this.arr = new int[row][col];
}
// Method to input matrix elements
public void input() {
Scanner sc = new Scanner(System.in);
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
arr[i][j] = sc.nextInt();
}
}
}
// Method to display the matrix
public void display(String message) {
System.out.println(message);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
// Method to sort the matrix column-wise in ascending order
public void sortCols() {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
for (int k = 0; k < row; k++) {
if (arr[i][j] < arr[k][j]) {
int temp = arr[i][j];
arr[i][j] = arr[k][j];
arr[k][j] = temp;
}
}
}
}
}
// Main method
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("ENTER THE ROW OF MATRIX");
int row = sc.nextInt();
System.out.println("ENTER THE COLUMN OF MATRIX");
int col = sc.nextInt();
MatColSortAcend matrix = new MatColSortAcend(row, col);
matrix.input();
matrix.display("UNSORTED MATRIX");
matrix.sortCols();
matrix.display("SORTED MATRIX");
}
}