Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use generic for pigeonholesort and max min Int #565

Merged
merged 3 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion math/max/max.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package max

import "github.com/TheAlgorithms/Go/constraints"

// Int is a function which returns the maximum of all the integers provided as arguments.
func Int(values ...int) int {
func Int[T constraints.Integer](values ...T) T {
max := values[0]
for _, value := range values {
if value > max {
Expand Down
4 changes: 3 additions & 1 deletion math/min/min.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package min

import "github.com/TheAlgorithms/Go/constraints"

// Int is a function which returns the minimum of all the integers provided as arguments.
func Int(values ...int) int {
func Int[T constraints.Integer](values ...T) T {
min := values[0]
for _, value := range values {
if value < min {
Expand Down
9 changes: 5 additions & 4 deletions sort/pigeonholesort.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
package sort

import (
"github.com/TheAlgorithms/Go/constraints"
"github.com/TheAlgorithms/Go/math/max"
"github.com/TheAlgorithms/Go/math/min"
)

// Pigeonhole sorts a slice using pigeonhole sorting algorithm.
func Pigeonhole(arr []int) []int {
func Pigeonhole[T constraints.Integer](arr []T) []T {
if len(arr) == 0 {
return arr
}
Expand All @@ -19,15 +20,15 @@ func Pigeonhole(arr []int) []int {

size := max - min + 1

holes := make([]int, size)

holes := make([]T, size)
// To maintain time complexity O(n + N), this is the reason for having only Integer constraint instead of Ordered.
VUHAILAM marked this conversation as resolved.
Show resolved Hide resolved
for _, element := range arr {
holes[element-min]++
}

i := 0

for j := 0; j < size; j++ {
for j := T(0); j < size; j++ {
for holes[j] > 0 {
holes[j]--
arr[i] = j + min
Expand Down
4 changes: 2 additions & 2 deletions sort/sorts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func TestComb(t *testing.T) {
}

func TestPigeonhole(t *testing.T) {
testFramework(t, sort.Pigeonhole)
testFramework(t, sort.Pigeonhole[int])
}

func TestPatience(t *testing.T) {
Expand Down Expand Up @@ -238,7 +238,7 @@ func BenchmarkComb(b *testing.B) {
}

func BenchmarkPigeonhole(b *testing.B) {
benchmarkFramework(b, sort.Pigeonhole)
benchmarkFramework(b, sort.Pigeonhole[int])
}

func BenchmarkPatience(b *testing.B) {
Expand Down