TheAlgorithms-Ruby/sorting/bubble_sort.rb

26 lines
396 B
Ruby
Raw Normal View History

2021-02-07 08:05:54 +01: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 unless swapped
end
array
end
2020-12-30 18:33:57 +01:00
if $0 == __FILE__
puts 'Enter a list of numbers separated by space'
list = gets
bubble_sort(list)
print list
end