-
Notifications
You must be signed in to change notification settings - Fork 2
/
Main.java
46 lines (34 loc) Β· 1.28 KB
/
Main.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
package Sort.P2751;
import java.io.*;
public class Main {
static int N;
static int[] arr;
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/Sort/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
N = Integer.parseInt(br.readLine());
arr = new int[N];
for (int i = 0; i < N; i++) arr[i] = Integer.parseInt(br.readLine());
mergeSort(0, N-1);
for (int n : arr) bw.write(n + "\n");
bw.flush(); bw.close(); br.close();
}
static void mergeSort(int start, int end) {
if (start == end) return;
mergeSort(start, (start+end)/2);
mergeSort((start+end)/2+1, end);
merge(start, (start+end)/2, end);
}
static void merge(int start, int mid, int end) {
int[] tmp = new int[end - start + 1];
int i = start, j = mid+1, k = 0;
while (i <= mid && j <= end) {
if (arr[i] < arr[j]) tmp[k++] = arr[i++];
else tmp[k++] = arr[j++];
}
while (i <= mid) tmp[k++] = arr[i++];
while (j <= end) tmp[k++] = arr[j++];
for (int t : tmp) arr[start++] = t;
}
}