mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-28 19:58:20 +01:00
Added Bubble Sort
This commit is contained in:
parent
cf04302347
commit
04d7fc3696
1 changed files with 22 additions and 0 deletions
22
BubbleSort.rb
Normal file
22
BubbleSort.rb
Normal file
|
@ -0,0 +1,22 @@
|
|||
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
|
||||
puts "Enter a list of numbers seprated by space"
|
||||
|
||||
list = gets
|
||||
bubble_sort(list)
|
||||
print list
|
Loading…
Add table
Reference in a new issue