Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,26 @@
|
|||
class Array
|
||||
def radix_sort(base=10)
|
||||
ary = dup
|
||||
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
|
||||
ary.each do |n|
|
||||
digit = (n/base_i) % base
|
||||
digit += base if 0<=n
|
||||
buckets[digit] << n
|
||||
end
|
||||
ary = buckets.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
|
||||
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