Created
August 10, 2019 12:51
-
-
Save luoheng23/a90b31e1248ee9bf4e6c0acdfbb05926 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
// CountSortForRadixSort O(n) | |
func CountSortForRadixSort(A []int, d int, k int) { | |
dNum := 1 | |
for i := 0; i < d; i++ { | |
dNum *= k | |
} | |
c := make([]int, 11) | |
for _, a := range A { | |
c[a/dNum%k]++ | |
} | |
for i := 1; i < 11; i++ { | |
c[i] += c[i-1] | |
} | |
b := make([]int, len(A)) | |
for j := len(A) - 1; j >= 0; j-- { | |
c[A[j]/dNum%k]-- | |
b[c[A[j]/dNum%k]] = A[j] | |
} | |
for i := range A { | |
A[i] = b[i] | |
} | |
} | |
// RadixSort O(d(n + k)) | |
func RadixSort(A []int, d int, k int) { | |
for i := 1; i < d+1; i++ { | |
CountSortForRadixSort(A, i, k) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment