File tree Expand file tree Collapse file tree 1 file changed +15
-4
lines changed Expand file tree Collapse file tree 1 file changed +15
-4
lines changed Original file line number Diff line number Diff line change @@ -8,16 +8,27 @@ package sort
8
8
9
9
import "github.com/TheAlgorithms/Go/constraints"
10
10
11
- func Count [T constraints.Number ](data []int ) []int {
12
- var aMin , aMax = - 1000 , 1000
11
+ func Count [T constraints.Integer ](data []T ) []T {
12
+ if len (data ) == 0 {
13
+ return data
14
+ }
15
+ var aMin , aMax = data [0 ], data [0 ]
16
+ for _ , x := range data {
17
+ if x < aMin {
18
+ aMin = x
19
+ }
20
+ if x > aMax {
21
+ aMax = x
22
+ }
23
+ }
13
24
count := make ([]int , aMax - aMin + 1 )
14
25
for _ , x := range data {
15
- count [x - aMin ]++ // this is the reason for having only Number constraint instead of Ordered.
26
+ count [x - aMin ]++ // this is the reason for having only Integer constraint instead of Ordered.
16
27
}
17
28
z := 0
18
29
for i , c := range count {
19
30
for c > 0 {
20
- data [z ] = i + aMin
31
+ data [z ] = T ( i ) + aMin
21
32
z ++
22
33
c --
23
34
}
You can’t perform that action at this time.
0 commit comments