Sorting algorithms/Counting sort: Difference between revisions

Line 1,571:
=={{header|Ruby}}==
<lang ruby>class Array
def counting_sort!
dup.replace counting_sort!
end
def counting_sort!
min, max = minmax
count = Array.new(max - min + 1, 0)
each {|number| count[number - min] += 1}
(min..max).each_with_object([]) {|i, ary| count[i - min].times {ary << i}}
z = 0
min.upto(max) do |i|
while count[i - min] > 0
self[z] = i
z += 1
count[i - min] -= 1
end
end
self
end
end
Anonymous user