-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ2.java
More file actions
69 lines (61 loc) · 1.43 KB
/
Q2.java
File metadata and controls
69 lines (61 loc) · 1.43 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
68
69
import java.util.Scanner;
public class Q2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter no of rows and columns");
int row = sc.nextInt();
int col = sc.nextInt();
System.out.println("Enter array elements");
int[][] arr = new int[row][col];
for(int i=0; i<row; i++) {
for(int j=0; j<col; j++) {
arr[i][j] = sc.nextInt();
}
}
System.out.println("Select sort\n 1.Increasing order\n 2.Decreasing order");
int num = sc.nextInt();
switch(num) {
case 1:
for(int i=0; i<row; i++) {
for(int j=0; j<col; j++) {
for(int m=0; m<row; m++) {
for(int n=0; n<col; n++) {
if(arr[i][j]<arr[m][n]) {
int temp = arr[i][j];
arr[i][j] = arr[m][n];
arr[m][n] = temp;
}
}
}
}
}
for(int i=0; i<row; i++) {
for(int j=0; j<col; j++) {
System.out.println(arr[i][j]);
}
}
break;
case 2:
for(int i=0; i<row; i++) {
for(int j=0; j<col; j++) {
for(int m=0; m<row; m++) {
for(int n=0; n<col; n++) {
if(arr[i][j]>arr[m][n]) {
int temp = arr[i][j];
arr[i][j] = arr[m][n];
arr[m][n] = temp;
}
}
}
}
}
for(int i=0; i<row; i++) {
for(int j=0; j<col; j++) {
System.out.println(arr[i][j]);
}
}
break;
default: System.out.println("Not available");
}
}
}