Okay, I got confused by how ruby passes it's arguments to a function. I've read in the documentation that ruby passes by value, but in my case it looks like it's passing by referance Here's the problem:
def buble_sort(arr)
unordered = true
while unordered
unordered = false
for i in (1..arr.size-1)
if(arr[i-1] > arr[i])
arr[i-1], arr[i] = arr[i], arr[i-1]
unordered = true;
end
end
end
arr
end
Calling this method shouldn't modify the arr value, since ruby passes it by value. But in my case, it does modify the original array. Why? Code:
p "#{arr} before sort" # => "[85, -4, 1, 2, 55, 23, 0] before sort"
p buble_sort(arr) # => [-4, 0, 1, 2, 23, 55, 85]
p "#{arr} after sort" # => "[-4, 0, 1, 2, 23, 55, 85] after sort"