File tree 4 files changed +14
-7
lines changed
4 files changed +14
-7
lines changed Original file line number Diff line number Diff line change 1
1
package max
2
2
3
+ import "github.com/TheAlgorithms/Go/constraints"
4
+
3
5
// Int is a function which returns the maximum of all the integers provided as arguments.
4
- func Int (values ... int ) int {
6
+ func Int [ T constraints. Integer ] (values ... T ) T {
5
7
max := values [0 ]
6
8
for _ , value := range values {
7
9
if value > max {
Original file line number Diff line number Diff line change 1
1
package min
2
2
3
+ import "github.com/TheAlgorithms/Go/constraints"
4
+
3
5
// Int is a function which returns the minimum of all the integers provided as arguments.
4
- func Int (values ... int ) int {
6
+ func Int [ T constraints. Integer ] (values ... T ) T {
5
7
min := values [0 ]
6
8
for _ , value := range values {
7
9
if value < min {
Original file line number Diff line number Diff line change 4
4
package sort
5
5
6
6
import (
7
+ "github.com/TheAlgorithms/Go/constraints"
7
8
"github.com/TheAlgorithms/Go/math/max"
8
9
"github.com/TheAlgorithms/Go/math/min"
9
10
)
10
11
11
12
// Pigeonhole sorts a slice using pigeonhole sorting algorithm.
12
- func Pigeonhole (arr []int ) []int {
13
+ // NOTE: To maintain time complexity O(n + N), this is the reason for having
14
+ // only Integer constraint instead of Ordered.
15
+ func Pigeonhole [T constraints.Integer ](arr []T ) []T {
13
16
if len (arr ) == 0 {
14
17
return arr
15
18
}
@@ -19,15 +22,15 @@ func Pigeonhole(arr []int) []int {
19
22
20
23
size := max - min + 1
21
24
22
- holes := make ([]int , size )
25
+ holes := make ([]T , size )
23
26
24
27
for _ , element := range arr {
25
28
holes [element - min ]++
26
29
}
27
30
28
31
i := 0
29
32
30
- for j := 0 ; j < size ; j ++ {
33
+ for j := T ( 0 ) ; j < size ; j ++ {
31
34
for holes [j ] > 0 {
32
35
holes [j ]--
33
36
arr [i ] = j + min
Original file line number Diff line number Diff line change @@ -132,7 +132,7 @@ func TestComb(t *testing.T) {
132
132
}
133
133
134
134
func TestPigeonhole (t * testing.T ) {
135
- testFramework (t , sort .Pigeonhole )
135
+ testFramework (t , sort .Pigeonhole [ int ] )
136
136
}
137
137
138
138
func TestPatience (t * testing.T ) {
@@ -238,7 +238,7 @@ func BenchmarkComb(b *testing.B) {
238
238
}
239
239
240
240
func BenchmarkPigeonhole (b * testing.B ) {
241
- benchmarkFramework (b , sort .Pigeonhole )
241
+ benchmarkFramework (b , sort .Pigeonhole [ int ] )
242
242
}
243
243
244
244
func BenchmarkPatience (b * testing.B ) {
You can’t perform that action at this time.
0 commit comments