Created
December 4, 2017 08:01
-
-
Save didasy/ccfc02a5731aa6a8d49d5549b0a66f93 to your computer and use it in GitHub Desktop.
Radix Sort Example for Go, taken from github.com/dgryski/go-radixsort
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 Ints(E []int) { | |
// Algorithm from CLRS, chapter 8 | |
const m = 256 | |
n := len(E) | |
counts := make([]int, m) | |
src := E | |
dst := make([]int, n) | |
for i := uint(0); i < 8; i++ { | |
shift := i * 8 | |
for i := range counts { | |
counts[i] = 0 | |
} | |
for i := 0; i < n; i++ { | |
j := byte(src[i] >> shift) | |
counts[j]++ | |
} | |
for j := 1; j < m; j++ { | |
counts[j] += counts[j-1] | |
} | |
for i := n - 1; i >= 0; i-- { | |
j := byte(src[i] >> shift) | |
dst[counts[j]-1] = src[i] | |
counts[j]-- | |
} | |
src, dst = dst, src | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment