Bubble Sort in Ruby

Published on 20 March 2019 (Updated: 07 May 2026)

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.

Current Solution

class Array
  def bubble_sort
    arr = dup
    n = arr.length

    (n - 1).times do |i|
      swapped = false

      (n - i - 1).times do |j|
        next unless arr[j] > arr[j + 1]

        arr[j], arr[j + 1] = arr[j + 1], arr[j]
        swapped = true
      end

      break unless swapped
    end

    arr
  end
end

def parse_input
  raw = ARGV.first
  raise ArgumentError unless raw

  numbers = raw.split(",").map { Integer(it.strip, exception: false) }
  raise ArgumentError if numbers.any?(nil) || numbers.length < 2

  numbers
end

def usage!
  abort %(Usage: please provide a list of at least two integers to sort in the format "1, 2, 3, 4, 5")
end

begin
  numbers = parse_input
  puts numbers.bubble_sort.join(", ")
rescue ArgumentError
  usage!
end

Bubble 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.