Ruby学习之路——编程实践【5】 2012-06-08
选择排序
def selectSort(arrInt, low = 0, high = arrInt.length - 1)
0.upto(high - 1) do |i|
small = i
i.upto(high) do |j|
if arrInt[j] < arrInt[small] then
small = j
end
end
if small != i then
arrInt[small], arrInt[i] = arrInt[i], arrInt[small]
end
end
end
a = Array.new
0.upto(100){ |i| a[a.length] = (Random.new.rand*10**2).to_i }
selectSort(a)
p a