-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaekjoon10989.java
49 lines (36 loc) · 1.2 KB
/
Baekjoon10989.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
package Algorithms;
import java.io.*;
import java.util.Scanner;
/**
* https://www.acmicpc.net/problem/10989
* 백준 수 정렬하기 카운팅정렬, 기수 정렬
*/
public class Baekjoon10989 {
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
int N = Integer.parseInt(br.readLine());
int[] numbers = new int[N];
int[] counting = new int[10001];
for (int i = 0; i < N; i++) {
numbers[i] = Integer.parseInt(br.readLine());
counting[numbers[i]]++;
}
int sum = 0;
for (int j = 0; j < counting.length; j++) {
if (counting[j] == 0) continue;
sum += counting[j];
counting[j] = sum;
}
int[] sorted = new int[N];
for (int i = N - 1; i >= 0; i--) {
int index = counting[numbers[i]] - 1;
counting[numbers[i]]--;
sorted[index] = numbers[i];
}
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
for (int n : sorted) {
bw.write(n+"\n");
}
bw.flush();
}
}