-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2DArraySort.java
More file actions
50 lines (39 loc) · 903 Bytes
/
2DArraySort.java
File metadata and controls
50 lines (39 loc) · 903 Bytes
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
/******************************************************************************
Sorting 2-Dimensional Array...!
Input:
4
2 3 6 1
4 5 10 11
14 16 7 9
8 12 15 13
Output:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
*******************************************************************************/
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner x=new Scanner(System.in);
int n=x.nextInt();
int[][] mat=new int[n][n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
mat[i][j]=x.nextInt();
// Flating the elements ans sorted..
int[] flat=Arrays.stream(mat).flatMapToInt(Arrays::stream).toArray();
Arrays.sort(flat);
int k=0;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
mat[i][j]=flat[k++];
System.out.println();
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)
System.out.print(mat[i][j]+" ");
System.out.println();
}
}
}