MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / Count

Function Count

sort/countingsort.go:14–40  ·  view source on GitHub ↗
(data []T)

Source from the content-addressed store, hash-verified

12import "github.com/TheAlgorithms/Go/constraints"
13
14func Count[T constraints.Integer](data []T) []T {
15 if len(data) == 0 {
16 return data
17 }
18 var aMin, aMax = data[0], data[0]
19 for _, x := range data {
20 if x < aMin {
21 aMin = x
22 }
23 if x > aMax {
24 aMax = x
25 }
26 }
27 count := make([]int, aMax-aMin+1)
28 for _, x := range data {
29 count[x-aMin]++ // this is the reason for having only Integer constraint instead of Ordered.
30 }
31 z := 0
32 for i, c := range count {
33 for c > 0 {
34 data[z] = T(i) + aMin
35 z++
36 c--
37 }
38 }
39 return data
40}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected