mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-26 21:58:56 +01:00
25 lines
396 B
Ruby
25 lines
396 B
Ruby
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
|
|
|
|
if $0 == __FILE__
|
|
puts 'Enter a list of numbers separated by space'
|
|
|
|
list = gets
|
|
bubble_sort(list)
|
|
print list
|
|
end
|