I am doing a simple selection sort in ruby but I am sorting in descending order. It’s confusing me because this work for all of my data except for one number.
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
unsorted data:
->
26.71%
0.17%
0.65%
0.36%
0.17%
2.79%
0.04%
18.03%
0.13%
25.7%
25.18%
0.36%
Iterations:
->
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.18%
25.7%
0.65%
0.36%
0.17%
2.79%
0.04%
18.03%
0.13%
0.17%
0.36%
18.03% gets skipped here:
->
26.71%2.79%
25.7%
25.18%
0.36%
0.17%
0.36%
0.04%
18.03%
0.13%
0.17%
0.65%
->
26.71%18.03%
25.7%
25.18%
2.79%
0.17%
0.36%
0.04%
0.13%
0.17%
0.65%
0.36%
->
26.71%0.65%
25.7%
25.18%
2.79%
18.03%
0.36%
0.04%
0.17%
0.13%
0.17%
0.36%
->
26.71%0.36%
25.7%
25.18%
2.79%
18.03%
0.65%
0.04%
0.17%
0.13%
0.17%
0.36%
->
26.71%0.36%
25.7%
25.18%
2.79%
18.03%
0.65%
0.36%
0.17%
0.13%
0.17%
0.04%
->
26.71%0.17%
25.7%
25.18%
2.79%
18.03%
0.65%
0.36%
0.36%
0.13%
0.04%
0.17%
->
26.71%0.17%
25.7%
25.18%
2.79%
18.03%
0.65%
0.36%
0.36%
0.17%
0.13%`
0.04%
->
26.71%0.13%`
25.7%
25.18%
2.79%
18.03%
0.65%
0.36%
0.36%
0.17%
0.17%
0.04%
I can’t figure out why 2.79% gets counted as > 18.03%
Any help would be great, Thanks!
Seems like you are sorting alphabetically so I would wager the data you are sorting are strings when you wanted them to be floats.