Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions sort/bucketsort.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
6 changes: 6 additions & 0 deletions sort/sorts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down