A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Selection 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 parse_input(str)
return if str.to_s.strip.empty?
nums = str.split(",").map { Integer(it.strip, exception: false) }
return if nums.any?(nil?) || nums.length < 2
nums
end
class Array
def selection_sort
arr = dup
sorted = []
until arr.empty?
min_index = arr.each_index.min_by { |i| arr[i] }
sorted << arr.delete_at(min_index)
end
sorted
end
end
USAGE = 'Usage: please provide a list of at least two integers to sort in the format "1, 2, 3, 4, 5"'
input = parse_input(ARGV.first)
abort(USAGE) unless input
puts input.selection_sort.join(", ")
Selection 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.