Skip to content

Instantly share code, notes, and snippets.

@luoheng23
Created August 10, 2019 12:45
Show Gist options
  • Save luoheng23/e0da88af5aeaa05030bc1ad7e1a77178 to your computer and use it in GitHub Desktop.
Save luoheng23/e0da88af5aeaa05030bc1ad7e1a77178 to your computer and use it in GitHub Desktop.
// CountSort O(n + k)
func CountSort(A []int, k int) {
c := make([]int, k+1)
for _, a := range A {
c[a]++
}
for i := 1; i < k+1; i++ {
c[i] += c[i-1]
}
b := make([]int, len(A))
for j := len(A) - 1; j >= 0; j-- {
c[A[j]]--
b[c[A[j]]] = A[j]
}
for i := range A {
A[i] = b[i]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment