def counting_sort(a) min = a.min max = a.max counts = Array.new(max-min+1, 0) a.each do |n| counts[n-min] += 1 end (0...counts.size).map{|i| [i+min]*counts[i]}.flatten end