Selection Sort in Ruby

Published on 29 October 2025 (Updated: 07 May 2026)

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.

Current Solution

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.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.