mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 22:28:40 +08:00 
			
		
		
		
	Update counting_sort.go and radix_sort.go
This commit is contained in:
		@ -8,7 +8,7 @@ type CountingSort struct{}
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/* 计数排序 */
 | 
					/* 计数排序 */
 | 
				
			||||||
// 简单实现,无法用于排序对象
 | 
					// 简单实现,无法用于排序对象
 | 
				
			||||||
func (c *CountingSort) countingSortNaive(nums []int) {
 | 
					func countingSortNaive(nums []int) {
 | 
				
			||||||
	// 1. 统计数组最大元素 m
 | 
						// 1. 统计数组最大元素 m
 | 
				
			||||||
	m := 0
 | 
						m := 0
 | 
				
			||||||
	for num := range nums {
 | 
						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
 | 
						// 1. 统计数组最大元素 m
 | 
				
			||||||
	m := 0
 | 
						m := 0
 | 
				
			||||||
	for num := range nums {
 | 
						for num := range nums {
 | 
				
			||||||
 | 
				
			|||||||
@ -10,11 +10,10 @@ import (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestCountingSort(t *testing.T) {
 | 
					func TestCountingSort(t *testing.T) {
 | 
				
			||||||
	c := &CountingSort{}
 | 
					 | 
				
			||||||
	nums := []int{1, 0, 1, 2, 0, 4, 0, 2, 2, 4}
 | 
						nums := []int{1, 0, 1, 2, 0, 4, 0, 2, 2, 4}
 | 
				
			||||||
	c.countingSortNaive(nums)
 | 
						countingSortNaive(nums)
 | 
				
			||||||
	fmt.Println("计数排序(无法排序对象)完成后 nums = ", nums)
 | 
						fmt.Println("计数排序(无法排序对象)完成后 nums = ", nums)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	c.countingSort(nums)
 | 
						countingSort(nums)
 | 
				
			||||||
	fmt.Println("计数排序完成后 nums = ", nums)
 | 
						fmt.Println("计数排序完成后 nums = ", nums)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -13,7 +13,7 @@ func digit(num, exp int) int {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* 计数排序(根据 nums 第 k 位排序) */
 | 
					/* 计数排序(根据 nums 第 k 位排序) */
 | 
				
			||||||
func countingSort(nums []int, exp int) {
 | 
					func countingSortDigit(nums []int, exp int) {
 | 
				
			||||||
	// 十进制的各位数字范围为 0~9 ,因此需要长度为 10 的桶
 | 
						// 十进制的各位数字范围为 0~9 ,因此需要长度为 10 的桶
 | 
				
			||||||
	bucket := make([]int, 10)
 | 
						bucket := make([]int, 10)
 | 
				
			||||||
	n := len(nums)
 | 
						n := len(nums)
 | 
				
			||||||
@ -56,6 +56,6 @@ func radixSort(nums []int) {
 | 
				
			|||||||
		// k = 2 -> exp = 10
 | 
							// k = 2 -> exp = 10
 | 
				
			||||||
		// k = 3 -> exp = 100
 | 
							// k = 3 -> exp = 100
 | 
				
			||||||
		// 即 exp = 10^(k-1)
 | 
							// 即 exp = 10^(k-1)
 | 
				
			||||||
		countingSort(nums, exp)
 | 
							countingSortDigit(nums, exp)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user