A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by rzuckerm
Welcome to the Bubble Sort in Ruby page! Here, you'll find the source code for this program as well as a description of how the program works.
def bubble_sort(numbers)
n = numbers.length
for i in 0...n-1
for j in 0...n-i-1
if numbers[j] > numbers[j + 1]
numbers[j], numbers[j + 1] = numbers[j + 1], numbers[j]
end
end
end
return numbers
end
def err()
puts('Usage: please provide a list of at least two integers to sort in the format "1, 2, 3, 4, 5"')
end
begin
unsorted = ARGV[0].split(", ").map{|i| Integer(i)}
if unsorted.length > 1
sorted = bubble_sort(unsorted)
print(sorted)
else
err()
end
rescue
err()
end
Bubble Sort in Ruby was written by:
If you see anything you'd like to change or update, please consider contributing.
Note: The solution shown above is the current solution in the Sample Programs repository as of Mar 20 2019 19:51:53. The solution was first committed on Mar 20 2019 15:25:56. As a result, documentation below may be outdated.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.