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 selection_sort(numbers)
# Handle missing or invalid input
if numbers.nil? || numbers.strip.empty?
return 'Usage: please provide a list of at least two integers to sort in the format "1, 2, 3, 4, 5"'
end
# Split into an array and convert to integers
unsorted_elements = numbers.split(',').map(&:strip).map(&:to_i)
# Validate array has at least 2 elements
if unsorted_elements.length < 2
return 'Usage: please provide a list of at least two integers to sort in the format "1, 2, 3, 4, 5"'
end
# Make an array to store the sorted elements
sorted_elements = []
# Iterate until the list of unsorted elements is emptu
until unsorted_elements.empty?
# Store the minimal value in a variable
min_element = unsorted_elements.min
# Add the element at the end of the sorted elements array
sorted_elements.push(min_element)
# Delete the minimal value from the unsorted elements array
unsorted_elements.delete_at(unsorted_elements.index(min_element))
end
# Return as comma-separated string
sorted_elements.join(', ')
end
puts selection_sort(ARGV[0])
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.