TheAlgorithms-Ruby/sorting/bubble_sort.rb

22 lines
377 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
2020-11-01 12:58:09 +01:00
puts "Enter a list of numbers separated by space"
2016-07-25 19:33:27 +02:00
2020-05-14 04:15:53 +02:00
list = gets
2016-07-25 19:33:27 +02:00
bubble_sort(list)
print list