Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/radix/sort.rb

Instance Method Summary collapse

Instance Method Details

#radix_sortObject



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/radix/sort.rb', line 6

def radix_sort
  input_array  = dup
  buckets      = Array.new(10) { Array.new }
  (Math.log10(input_array.max.abs).floor + 1).times { |index|
    input_array.each { |val|
      buckets[val % (10 ** (index + 1)) / (10 ** (index))] << val
    }
    input_array  = buckets.flatten
    buckets      = Array.new(10) { Array.new }
  }
  input_array
end

#radix_sort!Object



2
3
4
# File 'lib/radix/sort.rb', line 2

def radix_sort!
  replace radix_sort
end