Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -1,7 +1,7 @@
|
|||
class Array
|
||||
def radix_sort(base=10)
|
||||
ary = dup
|
||||
rounds = (Math.log(ary.minmax.map(&:abs).max)/Math.log(base)).ceil
|
||||
rounds = (Math.log(ary.minmax.map(&:abs).max)/Math.log(base)).floor + 1
|
||||
rounds.times do |i|
|
||||
buckets = Array.new(2*base){[]}
|
||||
base_i = base**i
|
||||
|
|
@ -23,3 +23,4 @@ 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
|
||||
p [100000, -10000, 400, 23, 10000].radix_sort
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
class Array
|
||||
def radix_sort(base=10)
|
||||
ary = dup
|
||||
m, max = 1, ary.minmax.map(&:abs).max
|
||||
while m <= max
|
||||
buckets = Array.new(base){[]}
|
||||
ary.each {|n| buckets[(n.abs / m) % base] << n}
|
||||
ary = buckets.flatten
|
||||
m *= base
|
||||
end
|
||||
ary.partition{|n| n<0}.inject{|minus,plus| minus.reverse + plus}
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue