mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-28 22:24:14 +01:00
25 lines
527 B
Ruby
25 lines
527 B
Ruby
def bubble_sort(array)
|
|
array_length = array.length
|
|
return array if array_length <= 1
|
|
unsorted_until_index = array_length - 1
|
|
sorted = false
|
|
until sorted
|
|
sorted = true
|
|
0.upto(unsorted_until_index - 1) do |i|
|
|
if array[i] > array[i+1]
|
|
array[i], array[i+1] = array[i+1], array[i]
|
|
sorted = false
|
|
end
|
|
end
|
|
unsorted_until_index -= 1
|
|
end
|
|
return array
|
|
end
|
|
|
|
if $0 == __FILE__
|
|
puts 'Enter a list of numbers separated by space'
|
|
|
|
list = gets
|
|
bubble_sort(list)
|
|
print list
|
|
end
|