Created
August 10, 2019 12:45
-
-
Save luoheng23/e0da88af5aeaa05030bc1ad7e1a77178 to your computer and use it in GitHub Desktop.
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
// 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