-
Notifications
You must be signed in to change notification settings - Fork 0
/
count.go
48 lines (40 loc) · 916 Bytes
/
count.go
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
package sort
import (
"golang.org/x/exp/constraints"
)
// Count sorts a slice of integer elements by counting the number of elements
// and positioning them in the correct order. The time complexity is O(n + k)
// where n is the number of elements and k is the maximum value. The space
// complexity is O(k) as we need to create a slice of length k to store the count
// of each elements.
func Count[V constraints.Integer](s []V, asc bool) {
out := make([]V, len(s))
if len(s) == 0 {
return
}
max := s[0]
for i := 1; i < len(s); i++ {
if max < s[i] {
max = s[i]
}
}
acc := make([]uint, int(max)+1)
for i := 0; i < len(s); i++ {
acc[s[i]]++
}
for i := 1; i <= int(max); i++ {
acc[i] += acc[i-1]
}
i := len(s) - 1
for i >= 0 {
if !asc {
out[uint(len(s))-acc[s[i]]] = s[i]
acc[s[i]] -= 1
} else {
out[acc[s[i]]-1] = s[i]
acc[s[i]] -= 1
}
i -= 1
}
copy(s, out)
}