all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
|
|
@ -0,0 +1,24 @@
|
|||
class Array
|
||||
def radix_sort(base=10)
|
||||
ary = dup
|
||||
rounds = (Math.log(self.max.abs)/Math.log(base)).ceil
|
||||
rounds.times do |i|
|
||||
buckets = Hash.new {|h,k| h[k] = []}
|
||||
ary.each do |n|
|
||||
digit = (n/base**i) % base
|
||||
digit = digit + base unless n<0
|
||||
buckets[digit] << n
|
||||
end
|
||||
ary = buckets.values_at(*(0..2*base)).compact.flatten
|
||||
p [i, ary] if $DEBUG
|
||||
end
|
||||
ary
|
||||
end
|
||||
def radix_sort!(base=10)
|
||||
replace radix_sort(base)
|
||||
end
|
||||
end
|
||||
|
||||
p [1, 3, 8, 9, 0, 0, 8, 7, 1, 6].radix_sort
|
||||
p [170, 45, 75, 90, 2, 24, 802, 66].radix_sort
|
||||
p [170, 45, 75, 90, 2, 24, -802, -66].radix_sort
|
||||
Loading…
Add table
Add a link
Reference in a new issue