选择排序混乱

时间:2011-07-20 18:03:32

标签: ruby sorting

我在ruby中进行简单的选择排序,但我按降序排序。这让我感到困惑,因为除了一个数字之外,这对我的所有数据都有效。

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%

任何帮助都会很棒,谢谢!

1 个答案:

答案 0 :(得分:3)

好像你按字母顺序排序所以当你想让它们成为浮点数时,我会下注你正在排序的数据。