diff --git a/sort/bucketsort.go b/sort/bucketsort.go index 3f05a41a4..1ad7eabf1 100644 --- a/sort/bucketsort.go +++ b/sort/bucketsort.go @@ -22,6 +22,12 @@ func Bucket[T constraints.Number](arr []T) []T { } } + // all elements are equal, so arr is already sorted + // (and max - min below would be zero) + if max == min { + return arr + } + // create an empty bucket for each element in arr bucket := make([][]T, len(arr)) diff --git a/sort/sorts_test.go b/sort/sorts_test.go index f2394c334..d86e80e20 100644 --- a/sort/sorts_test.go +++ b/sort/sorts_test.go @@ -51,6 +51,12 @@ func testFramework(t *testing.T, sortingFunction func([]int) []int) { expected: []int{-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 10}, name: "Random order Signed", }, + //All elements equal + { + input: []int{5, 5, 5, 5}, + expected: []int{5, 5, 5, 5}, + name: "All Equal", + }, //Single-entry slice { input: []int{1},