-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharraysort2d.java
More file actions
102 lines (70 loc) · 1.98 KB
/
Copy patharraysort2d.java
File metadata and controls
102 lines (70 loc) · 1.98 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import java.util.*;
public class arraysort2d {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int rows = sc.nextInt();
int columns = sc.nextInt();
int arr[][] = new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
int num = sc.nextInt();
arr[i][j] = num;
}
}
System.out.println("Original 2D array: ");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
System.out.println("Enter 1 for ascending order and 2 for descending order");
int choice = sc.nextInt();
sc.close();
switch(choice) {
case 1:
for (int i = 0; i < rows; i++) {
for (int j = 0; j < rows; j++) {
for (int k = 0; k < columns; k++) {
for (int l = 0; l < columns; l++) {
if (arr[i][k] < arr[j][l]) {
int temp = arr[i][k];
arr[i][k] = arr[j][l];
arr[j][l] = temp;
}
}
}
}
}
System.out.println("Sorted 2D array:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
break;
case 2: for (int i = 0; i < rows; i++) {
for (int j = 0; j < rows; j++) {
for (int k = 0; k < columns; k++) {
for (int l = 0; l < columns; l++) {
if (arr[i][k] > arr[j][l]) {
int temp = arr[i][k];
arr[i][k] = arr[j][l];
arr[j][l] = temp;
}
}
}
}
}
System.out.println("Sorted 2D array:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
break;
}
}
}