我正在用红宝石做一个简单的选择排序,但我是按降序排序的。这让我很困惑,因为这对我所有的数据都有用,除了一个数字。
def parallel_sort(data, labels)
raise "unequal array lengths" if data.length != labels.length
temp = File.open("temp.txt", "w")
0.upto(data.length - 2) do |i|
max = i # largest value
(i+1).upto(data.length - 1) { |j| max = j if data[j] > data[max] }
data[i], data[max], labels[i], labels[max] = data[max], data[i], labels[max], labels[i] if i != max
temp.puts "->", data, "\n", labels, "\n"
end
return [data, labels]
end未排序数据:
-> 26.71% 0.17% 0.65% 0.36% 0.17% 2.79% 0.04% 18.03% 0.13% 25.7% 25.18% 0.36%
迭代:
-> 26.71% 25.7% 0.65% 0.36% 0.17% 2.79% 0.04% 18.03% 0.13% 0.17% 25.18% 0.36%
-> 26.71% 25.7% 25.18% 0.36% 0.17% 2.79% 0.04% 18.03% 0.13% 0.17% 0.65% 0.36%
18.03%在这里跳过:
-> 26.71% 25.7% 25.18% 2.79% 0.17% 0.36% 0.04% 18.03% 0.13% 0.17% 0.65% 0.36%
-> 26.71% 25.7% 25.18% 2.79% 18.03% 0.36% 0.04% 0.17% 0.13% 0.17% 0.65% 0.36%
-> 26.71% 25.7% 25.18% 2.79% 18.03% 0.65% 0.04% 0.17% 0.13% 0.17% 0.36% 0.36%
-> 26.71% 25.7% 25.18% 2.79% 18.03% 0.65% 0.36% 0.17% 0.13% 0.17% 0.04% 0.36%
-> 26.71% 25.7% 25.18% 2.79% 18.03% 0.65% 0.36% 0.36% 0.13% 0.17% 0.04% 0.17%
-> 26.71% 25.7% 25.18% 2.79% 18.03% 0.65% 0.36% 0.36% 0.17% 0.13% 0.04% 0.17%
-> 26.71% 25.7% 25.18% 2.79% 18.03% 0.65% 0.36% 0.36% 0.17% 0.17% 0.04% 0.13%‘
-> 26.71% 25.7% 25.18% 2.79% 18.03% 0.65% 0.36% 0.36% 0.17% 0.17% 0.13%‘0.04%
我不明白为什么2.79%被计算为> 18.03%
任何帮助都会很好,谢谢!
发布于 2011-07-20 18:41:26
看起来你是按字母顺序排序的,所以我敢打赌,当你想让它们浮起来的时候,你正在排序的数据是字符串。
https://stackoverflow.com/questions/6766344
复制相似问题