A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
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.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.