TheAlgorithms-Ruby/Sorting/bubble_sort.rb

22 lines
376 B
Ruby
Raw Normal View History

2016-07-25 19:33:27 +02:00
def bubble_sort(array)
n = array.length
loop do
swapped = false
(n-1).times do |i|
if array[i] > array[i+1]
array[i], array[i+1] = array[i+1], array[i]
swapped = true
end
end
break if not swapped
end
array
end
puts "Enter a list of numbers seprated by space"
2020-05-14 04:15:53 +02:00
list = gets
2016-07-25 19:33:27 +02:00
bubble_sort(list)
print list