TheAlgorithms-Ruby/sorting/bubble_sort.rb

22 lines
377 B
Ruby
Raw Normal View History

2016-07-25 23:03:27 +05:30
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 20:58:09 +09:00
puts "Enter a list of numbers separated by space"
2016-07-25 23:03:27 +05:30
2020-05-14 11:15:53 +09:00
list = gets
2016-07-25 23:03:27 +05:30
bubble_sort(list)
print list