mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 13:56:28 +08:00
Update counting_sort.go and radix_sort.go
This commit is contained in:
parent
b9e97d3823
commit
a78365401e
3 changed files with 8 additions and 7 deletions
|
@ -8,7 +8,7 @@ type CountingSort struct{}
|
|||
|
||||
/* 计数排序 */
|
||||
// 简单实现,无法用于排序对象
|
||||
func (c *CountingSort) countingSortNaive(nums []int) {
|
||||
func countingSortNaive(nums []int) {
|
||||
// 1. 统计数组最大元素 m
|
||||
m := 0
|
||||
for num := range nums {
|
||||
|
@ -31,7 +31,9 @@ func (c *CountingSort) countingSortNaive(nums []int) {
|
|||
}
|
||||
}
|
||||
|
||||
func (c *CountingSort) countingSort(nums []int) {
|
||||
/* 计数排序 */
|
||||
// 完整实现,可排序对象,并且是稳定排序
|
||||
func countingSort(nums []int) {
|
||||
// 1. 统计数组最大元素 m
|
||||
m := 0
|
||||
for num := range nums {
|
||||
|
|
|
@ -10,11 +10,10 @@ import (
|
|||
)
|
||||
|
||||
func TestCountingSort(t *testing.T) {
|
||||
c := &CountingSort{}
|
||||
nums := []int{1, 0, 1, 2, 0, 4, 0, 2, 2, 4}
|
||||
c.countingSortNaive(nums)
|
||||
countingSortNaive(nums)
|
||||
fmt.Println("计数排序(无法排序对象)完成后 nums = ", nums)
|
||||
|
||||
c.countingSort(nums)
|
||||
countingSort(nums)
|
||||
fmt.Println("计数排序完成后 nums = ", nums)
|
||||
}
|
|
@ -13,7 +13,7 @@ func digit(num, exp int) int {
|
|||
}
|
||||
|
||||
/* 计数排序(根据 nums 第 k 位排序) */
|
||||
func countingSort(nums []int, exp int) {
|
||||
func countingSortDigit(nums []int, exp int) {
|
||||
// 十进制的各位数字范围为 0~9 ,因此需要长度为 10 的桶
|
||||
bucket := make([]int, 10)
|
||||
n := len(nums)
|
||||
|
@ -56,6 +56,6 @@ func radixSort(nums []int) {
|
|||
// k = 2 -> exp = 10
|
||||
// k = 3 -> exp = 100
|
||||
// 即 exp = 10^(k-1)
|
||||
countingSort(nums, exp)
|
||||
countingSortDigit(nums, exp)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue