I\'m trying to use the bubble sort method to sort an array of only three numbers. The code I\'m using is below.
def my_sort(list)
return list if list.
Your code works for that specific array. Because your loop is looking if the next element is higher, then swipe. But what about more elements in the array? This is recursive solution for all cases.
def my_sort(list, new_array = nil)
return new_array if list.size <= 0
if new_array == nil
new_array = []
end
min = list.min
new_array << min
list.delete(min)
my_sort(list, new_array)
end
puts my_sort([3, 1, 2, 20, 11, 14, 3, 6, 8, 5])