Reputation: 39
def bubble_sort(arr)
sorted = false
until sorted
sorted = true
(0...arr.length-2).each do |i|
if arr[i] > arr[i+1]
arr[i], arr[i+1] = arr[i+1], arr[i]
sorted = false
end
end
end
arr
end
bubble_sort([4,6,2,8,1])
The following is the error message:
(eval):367: (eval):367: compile error (SyntaxError)
(eval):359: syntax error, unexpected kDO_COND, expecting kEND
(0...arr.length-2).each do |i|
^
(eval):367: syntax error, unexpected kEND, expecting $end
I don't understand why it is saying unexpected kEND
Upvotes: 0
Views: 36
Reputation: 59611
This appears to be a bug in Ruby 1.8.7 - replacing do/end
with {}
:
(0...arr.length-2).each do |i|
if arr[i] > arr[i+1]
arr[i], arr[i+1] = arr[i+1], arr[i]
sorted = false
end
end
and
(0...arr.length-2).each { |i|
if arr[i] > arr[i+1]
arr[i], arr[i+1] = arr[i+1], arr[i]
sorted = false
end
}
fixes it.
Upvotes: 1