我試圖使用氣泡排序方法對僅有三個數字的數組進行排序。我使用的代碼如下。在不使用排序方法的Ruby中對數組進行排序
def my_sort(list)
return list if list.size <= 1
swapped = false
while !swapped
swapped = false
0.upto(list.size-2) do |i|
if list[i] > list[i+1]
list[i], list[i+1] = list[i+1], list[i]
swapped = true
end
end
list
end
my_sort([3,1,2])
以下是錯誤消息我不斷收到:不應該包括
Syntax error, unexpected $end, expecting keyword_end
我只是想知道這到底?
縮進的代碼正確,你會看到丟失的'end'的時候了。 – tokland
可能的重複[你如何排序而不使用排序方法?](http://stackoverflow.com/questions/11057381/how-do-you-sort-without-using-the-sort-method) –