Last active
November 26, 2020 10:01
-
-
Save garethng/a0ccc3287bc85315521e25fa6227063c to your computer and use it in GitHub Desktop.
radixSort golang 基数排序
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func radixSort(nums []int) []int{ | |
maxValue := maxValueOfList(nums...) | |
a := 1 | |
for k:=0;a<=maxValue;k++{ | |
buckets := map[int][]int{} | |
for i:=0;i<10;i++{ | |
buckets[i] = []int{} | |
} | |
for _,num := range nums{ | |
d := num / a % 10 | |
buckets[d] = append(buckets[d], num) | |
} | |
nums = []int{} | |
for v:=0;v<10;v++{ | |
nums = append(nums, buckets[v]...) | |
} | |
a *= 10 | |
} | |
return nums | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment