-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortedmatrix.java
More file actions
42 lines (41 loc) · 1.07 KB
/
Copy pathsortedmatrix.java
File metadata and controls
42 lines (41 loc) · 1.07 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
package matrix;
import java.util.Scanner;
import java.util.Arrays;
public class sortedmatrix {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n;
System.out.println("enter the number of rows and columns : ");
n=s.nextInt();
int arr[][]=new int[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.println("enter the element : "+(i+1)+" "+(j+1));
arr[i][j]=s.nextInt();
}
}
int flatarr[]=new int[n*n];
int index=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
flatarr[index]=arr[i][j];
index++;
}
}
Arrays.sort(flatarr);
index=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(flatarr[index]);
index++;
}
System.out.println();
}
}
}