-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sorter.java
153 lines (129 loc) · 4.42 KB
/
Sorter.java
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import java.io.*;
import java.nio.file.Files;
import java.util.Scanner;
public class Sorter {
public File sortFile(File dataFile) throws IOException {
File a = new File(dataFile.getPath());
File b = new File(a.getParent(), "b.txt");
File c = new File(a.getParent(), "c.txt");
long iteration = 1;
while (true) {
if (split(a, b, c, iteration) == 1) {
break;
} else {
iteration = merge(a, b, c, iteration);
}
System.gc();
}
Files.delete(b.toPath());
Files.delete(c.toPath());
return a;
}
private long split(File a, File b, File c, long iteration) throws IOException {
long resault = 1;
long count = 0;
long length = getLongsQty(a);
long pointer = 0;
byte factor = 1;
if (length < 2) {
return resault;
}
try (Scanner scanner = new Scanner(new FileInputStream(a));
PrintWriter writerB = new PrintWriter(b);
PrintWriter writerC = new PrintWriter(c)){
while (pointer < length) {
if (count == iteration) {
count = 0;
resault++;
factor *= -1;
}
long number = scanner.nextLong();
pointer++;
if (factor == 1) {
writerB.println(number);
} else {
writerC.println(number);
}
count++;
}
writerB.flush();
writerC.flush();
}
return resault;
}
private long merge(File a, File b, File c, long iteration) throws IOException {
long lengthB = getLongsQty(b);
long lengthC = getLongsQty(c);
long countB = iteration;
long countC = iteration;
long number1 = 0;
long number2 = 0;
long pointerB = 0;
long pointerC = 0;
boolean isGotB = false;
boolean isGotC = false;
boolean isBEnds = false;
boolean isCEnds = false;
try (PrintWriter writer = new PrintWriter(a);
Scanner scannerB = new Scanner(new FileInputStream(b));
Scanner scannerC = new Scanner(new FileInputStream(c))) {
while (!isBEnds || !isCEnds) {
if (countB == 0 && countC == 0) {
countB = iteration;
countC = countB;
}
if (pointerB < lengthB) {
if (countB > 0 && !isGotB) {
number1 = scannerB.nextLong();
isGotB = true;
pointerB++;
}
} else {
isBEnds = true;
}
if (pointerC < lengthC) {
if (countC > 0 && !isGotC) {
number2 = scannerC.nextLong();
isGotC = true;
pointerC++;
}
} else {
isCEnds = true;
}
if (isGotB) {
if (isGotC) {
if (number1 < number2) {
writer.println(number1);
isGotB = false;
countB--;
} else {
writer.println(number2);
isGotC = false;
countC--;
}
} else {
writer.println(number1);
isGotB = false;
countB--;
}
} else if (isGotC) {
writer.println(number2);
isGotC = false;
countC--;
}
}
writer.flush();
}
return iteration * 2;
}
private long getLongsQty(File a) throws IOException {
long resault = 0;
try (Scanner scanner = new Scanner(new FileInputStream(a))) {
while (scanner.hasNextLong()) {
scanner.nextLong();
resault++;
}
}
return resault;
}
}