2011-07-20 81 views
1

我在做一個簡單的選擇排序在紅寶石,但我按降序排序。這讓我感到困惑,因爲除了一個數字之外,這個工作適用於我所有的數據。選擇排序混淆

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%

任何幫助將是偉大的,謝謝!

回答

3

好像你是按字母順序排序,所以我會打賭你正在排序的數據是字符串,當你想他們是浮動。

+0

嘆......謝謝。 –