-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagonalSort.java
More file actions
84 lines (66 loc) · 2.57 KB
/
Copy pathDiagonalSort.java
File metadata and controls
84 lines (66 loc) · 2.57 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
// https://jamboard.google.com/d/1Cvtyvn4QiMmAU3Cc2732PhOsl10VgZOoO7q90SIo6Xo/viewer?mtt=6m4owt9wocw3&f=0
import java.util.Scanner;
public class DiagonalSort {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
int arr[][] = new int[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
arr[i][j] = sc.nextInt();
}
}
int row = 0;
int col = 0;
while (true) {
System.out.println("row:" + row + " col:" + col);
if (!(row == 0 && col == 0) && !(row == arr.length - 1 && col == arr.length - 1)) {
int holdrow = row;
int holdcol = col;
int itrrow = row - 1;
int itrcol = col + 1;
while ((holdrow > 0 && holdcol < arr.length - 1)) {
System.out.println("holdRow:" + holdrow + " holdCol:" + holdcol);
System.out.println("arr[holdrow][holdcol]: " + arr[holdrow][holdcol]);
System.out.println("arr[itrrow][itrcol]: " + arr[itrrow][itrcol]);
if (arr[holdrow][holdcol] > arr[itrrow][itrcol]) {
int temp = arr[holdrow][holdcol];
arr[holdrow][holdcol] = arr[itrrow][itrcol];
arr[itrrow][itrcol] = temp;
System.out.println("swaped");
}
if (itrrow == col && itrcol == row) {
holdrow--;
holdcol++;
itrrow = holdrow - 1;
itrcol = holdcol + 1;
}
else if (itrrow > 0 && itrcol < arr.length - 1) {
itrrow--;
itrcol++;
}
}
}
if (row == arr.length - 1 && col == arr.length - 1) {
System.out.println("break");
break;
}
if (row == arr.length - 1) {
System.out.println("col++");
col++;
}
if (row < arr.length - 1) {
System.out.println("row++");
row++;
}
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
System.out.println("done");
sc.close();
}
}