Skip to content

Instantly share code, notes, and snippets.

@Nirma
Last active August 29, 2015 14:19
Show Gist options
  • Save Nirma/b56a6efa9d377bf762f7 to your computer and use it in GitHub Desktop.
Save Nirma/b56a6efa9d377bf762f7 to your computer and use it in GitHub Desktop.
Radix Sort in Ruby
def radix (a, base = 10)
m = a.max
exp = 1
loop do
bkt = Array.new(base){[]}
a.each{|elem|
pos = (elem / exp) % base
bkt[pos] << elem
}
a = bkt.flatten
exp *= base
break if exp > m
end
a
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment