package main
import (
"fmt"
)
func countingSort(array []int, k int) {
b, t := make([]int, len(array)), make([]int, k)
for i := 0; i < len(array); i++ {
t[array[i]]++
}
for i := 1; i < k; i++ {
t[i] += t[i-1]
}
for j := len(array) - 1; j >= 0; j-- {
b[t[array[j]]-1] = array[j]
t[array[j]]--
}
for i := 0; i < len(array); i++ {
array[i] = b[i]
}
}
func main() {
arr := []int{16, 4, 10, 14, 7, 9, 3, 2, 8, 1}
fmt.Println(arr)
countingSort(arr, 17)
fmt.Println(arr)
}
image.png
网友评论