Skip to content

Commit de46d60

Browse files
committed
feat: add bucket sort
1 parent 423457d commit de46d60

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,7 @@ list.sort(Comparator.comparingInt(String::length));
11721172
| **Worst** | $O(n^2)$ | when the input list is already sorted in the reverse order of the desired sorting order |
11731173
| **Average** | $O(n^2)$ | when the input list is in jumbled order |
11741174

1175-
- Bucket sort, CLRS#8.4: [java](/java-algorithm/src/main/java/com/example/algorithm/sort/BucketSort.java) | Bucket sort is a sorting algorithm that works by distributing the elements of an array into a number of buckets. Each bucket contains a range of values and the elements are sorted within these buckets using any of the suitable sorting algorithms (such as insertion sort, merge sort, selection sort).<br>($\textit{n}$ is the number of elements and $\textit{k}$ is the number of buckets)
1175+
- Bucket sort, CLRS#8.4: [golang](go-algorithm/pkg/sort/bucket_sort.go), [java](/java-algorithm/src/main/java/com/example/algorithm/sort/BucketSort.java) | Bucket sort is a sorting algorithm that works by distributing the elements of an array into a number of buckets. Each bucket contains a range of values and the elements are sorted within these buckets using any of the suitable sorting algorithms (such as insertion sort, merge sort, selection sort).<br>($\textit{n}$ is the number of elements and $\textit{k}$ is the number of buckets)
11761176

11771177
| **Case** | **Time complexity** | **Remarks** |
11781178
| ----------- | :-----------------: | -------------------------------------------------------------------------------------------------------------- |

go-algorithm/pkg/sort/bucket_sort.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package sort
2+
3+
import (
4+
"math"
5+
6+
"golang.org/x/exp/constraints"
7+
)
8+
9+
func bucketSort[T constraints.Integer](arr []T) []T {
10+
n := len(arr)
11+
12+
var maxVal, minVal T
13+
for _, v := range arr {
14+
if v > maxVal {
15+
maxVal = v
16+
}
17+
if v < minVal {
18+
minVal = v
19+
}
20+
}
21+
rangeVal := maxVal - minVal
22+
23+
bucket := make([][]T, n)
24+
for i := 0; i < n; i++ {
25+
bucket[i] = make([]T, 0)
26+
}
27+
28+
for _, v := range arr {
29+
index := int(math.Floor(float64(n) * float64((v-minVal)/rangeVal)))
30+
if index == n {
31+
index--
32+
}
33+
bucket[index] = append(bucket[index], v)
34+
}
35+
36+
for i := 0; i < n; i++ {
37+
insertionSort(bucket[i])
38+
}
39+
40+
sorted := make([]T, 0, n)
41+
for i := 0; i < n; i++ {
42+
sorted = append(sorted, bucket[i]...)
43+
}
44+
45+
return sorted
46+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package sort
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func Test_bucketSortInteger(t *testing.T) {
10+
type args struct {
11+
arr []int
12+
}
13+
tests := []struct {
14+
name string
15+
args args
16+
want []int
17+
}{
18+
{
19+
name: "Integer Case",
20+
args: args{
21+
arr: []int{31, 64, 49, 85, 71, 26, 6, 19},
22+
},
23+
want: []int{6, 19, 26, 31, 49, 64, 71, 85},
24+
},
25+
{
26+
name: "Empty array input",
27+
args: args{
28+
arr: []int{},
29+
},
30+
want: []int{},
31+
},
32+
}
33+
34+
for _, tt := range tests {
35+
t.Run(tt.name, func(t *testing.T) {
36+
got := bucketSort(tt.args.arr)
37+
38+
assert.Equalf(t, tt.want, got, "bucketSort() got = %v, want %v", got, tt.want)
39+
})
40+
}
41+
}

0 commit comments

Comments
 (0)