Skip to content

bucketSort implemented by go language #21

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

Closed
wants to merge 2 commits into from
Closed
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
82 changes: 81 additions & 1 deletion 10.radixSort.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,84 @@ function radixSort($arr, $maxDigit = null)

return $arr;
}
```
```

## 6. go 代码实现
```go
package main
import (
"fmt"
)

type radixSort struct {
length int //序列中最大数的位数
radix [][]int //0-9的10个桶
nums []int //要排序的序列
}

func (r *radixSort ) Init(numbers []int) {
r.nums = numbers
r.initLen()
}
//初始化最大位数
func (r *radixSort) initLen() {
size := len(r.nums)
max := r.nums[0]
for i := 1; i < size; i++ {
if r.nums[i] > max {
max = r.nums[i]
}
}
r.length = 1
max = max / 10
for max > 0 {
r.length++
max = max / 10
}
}

//输出序列
func (r *radixSort) Display() {
for _, v := range r.nums {
fmt.Printf("%d\t", v)
}
fmt.Print("\n")
}

//排序函数
func (r *radixSort) Sort() {
size := len(r.nums)
var i, j, k int
m := 1
//循环次数为常数,即序列中最大数的位数
for i = 1; i <= r.length; i++ {

r.radix = make([][]int, 10)

//遍历要排序的序列,将相应位数的元素加入到对应的位桶中
for j = 0; j < size; j++ {
k = r.nums[j] / m % 10
r.radix[k] = append(r.radix[k], r.nums[j])
}
//清空原序列数组
r.nums = make([]int, 0)

//将9-0各位桶的数重新组装放到原序列数组当中
for j = 9; j >= 0; j-- {
r.nums = append(r.nums, r.radix[j]...)
}
m = m * 10
r.Display()
}


}


func main() {
r := new(radixSort)
r.Init([]int{430, 122, 332, 167, 899, 998, 455, 691, 571})
r.Sort()
}

```
66 changes: 65 additions & 1 deletion 9.bucketSort.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,68 @@ function bucketSort($arr, $bucketSize = 5)

return $arr;
}
```
```

## 6. GO 代码实现
```go

func bucketSort(arr []int, bucketSize int) []int {

length := len(arr)
if length == 0 {
return arr
}
max := arr[0]
min := arr[0]

for i := 0; i < length; i++ {
if arr[i] < min {
min = arr[i]
} else if arr[i] > max {
max = arr[i]
}
}

bucketCount := (max - min) / bucketSize + 1
buckets := make([][]int,bucketCount)

// 利用映射函数将数据分配到各个桶中
for i := 0; i < length; i++ {
index := (arr[i] - min) / bucketSize
buckets[index] = append(buckets[index],arr[i])
}

arrIndex := 0
for _,bucket := range buckets {

if len(bucket) <= 0 {
continue
}
bucket = insertSort(bucket)

for _,v := range bucket {
arr[arrIndex] = v
arrIndex++
}

}

return arr
}

func insertSort(arr []int) []int {
for i := range arr {
preIndex := i - 1
current := arr[i]
for preIndex >= 0 && arr[preIndex] > current {
arr[preIndex+1] = arr[preIndex]
preIndex -= 1
}
if i != preIndex + 1 {
arr[preIndex+1] = current
}
}
return arr
}

```