Last active
December 24, 2015 10:38
-
-
Save rorhug/6785182 to your computer and use it in GitHub Desktop.
radix lsd sort in ruby http://en.wikipedia.org/wiki/Radix_sort#Least_significant_digit_radix_sorts
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
def lrgst(a) | |
largest = 0 | |
a.each {|i| largest = i if i > largest } | |
largest | |
end | |
def radix_lsd(uns) | |
sorted = (0..Math.log10(lrgst(uns)).floor).inject(uns) do |memo, index| | |
groups = Array.new(10).map{|x| [] } | |
memo.each { |u| groups[(u / 10**index) % 10] << u } | |
groups.flatten | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment