所以我试图学习如何在不使用.sort方法的情况下对数组进行排序,这是迄今为止我所拥有的,但是中间数字已经退出了。在ruby中插入排序算法遇到问题
def my_sort(num)
for j in 1...num.length
key = num[j]
i = j - 1
while i > 0 and num[i] = key
num[i+1] = num[i]
i = i - 1
end
num[i+1] = key
end
end
然后我运行的方法
my_sort([3,1,2])
我得到
=> 1...3
,但我想
=> 1,2,3
我在做什么错?
http://stackoverflow.com/a/38858611/1623261 –