-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbucket_sort.go
More file actions
44 lines (38 loc) · 836 Bytes
/
bucket_sort.go
File metadata and controls
44 lines (38 loc) · 836 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"fmt"
"slices"
"math/rand/v2"
)
func GenerateArray(size, minNumber, maxNumber int) []int {
array := make([]int, size);
for index := range array {
array[index] = rand.IntN(maxNumber - minNumber + 1) + minNumber;
}
return array
}
func BucketSort(array []int) {
// this method works with go 1.21+
maxValue := slices.Max(array)
bucket := make([]int, maxValue + 1)
for _, value := range array {
bucket[value]++;
}
original_it := 0
for bucket_it := 0;
bucket_it <= maxValue &&
original_it < len(array);
bucket_it ++ {
for bucket[bucket_it] > 0 {
array[original_it] = bucket_it;
bucket[bucket_it]--;
original_it++;
}
}
}
func main(){
numbers := GenerateArray(10, 0, 100)
fmt.Println("Original Array: ", numbers)
BucketSort(numbers)
fmt.Println("Sorted Array: ", numbers)
}