-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounting.java
More file actions
37 lines (26 loc) · 724 Bytes
/
Counting.java
File metadata and controls
37 lines (26 loc) · 724 Bytes
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
import java.util.Arrays;
public class Counting {
private static final int[] freq = new int[5001];
public static void sort(int[] a) {
int N = a.length;
int[] sorted = new int[N];
Arrays.fill(freq, 0);
for (int v: a) {
freq[v]++;
}
for (int i = 1; i < 5001; i++) {
freq[i] += freq[i-1];
}
for (int i = N-1; i > -1; i--) {
sorted[--freq[a[i]]] = a[i];
}
System.arraycopy(sorted, 0, a, 0, N);
}
public static void main(String[] args) {
int[] a = new int[] {3, 2, 7, 6, 4, 5, 1, 9};
sort(a);
for (int v: a) {
System.out.println(v);
}
}
}