Quick actions

cmd+k|ctrl+k

Navigation

Languages

Radix Sort Algorithm

Snippet info

Language

Ruby

Visibility

public

Author

yoneda

Created

2017-03-18T09:18:58Z

Updated

2017-08-06T20:55:59Z

def radixSort(list)
	maxLen = list.max_by(&:size)
	maxLen.times do |j|
		buckets=(0..9).map{ [] }
		list.each do |i|
			buckets[i.to_s[-j - 1].to_i] << i
		end
		list = buckets.select{|i| !i.empty? }.reduce(:+)
	end
	list
end
nums = 20.times.map{ rand 100 }
puts nums.inspect
puts radixSort(nums).inspect
INFO