EN
Java - sort 2D array
5 points
Using Java it is possible to sort 2D array in following way.
xxxxxxxxxx
1
package com.dirask.example;
2
3
public class Program {
4
5
public static void main(String[] args) {
6
int[][] array = {
7
{ 5, 3, 1 },
8
{ 8, -2, 0 },
9
{ 3, 7, 6 },
10
{ 9, 9, -3 }
11
};
12
13
// bubble sort example
14
for (int i = 0; i < 4 * 3; ++i) {
15
for (int j = 0; j < 4 * 3; ++j) {
16
int a = array[i / 3][i % 3];
17
int b = array[j / 3][j % 3];
18
19
if(a < b) {
20
array[i / 3][i % 3] = b;
21
array[j / 3][j % 3] = a;
22
}
23
}
24
}
25
26
for (int i = 0; i < 4; ++i) {
27
for (int j = 0; j < 3; ++j) {
28
System.out.print(" " + array[i][j]);
29
}
30
}
31
}
32
}
Output:
xxxxxxxxxx
1
-3 -2 0 1 3 3 5 6 7 8 9 9