A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Quick Sort in Ruby page! Here, you'll find the source code for this program as well as a description of how the program works.
# frozen_string_literal: true
def usage!
abort %(Usage: please provide a list of at least two integers to sort in the format "1, 2, 3, 4, 5")
end
def parse_input
raw = ARGV.first
usage! if raw.nil? || raw.strip.empty?
numbers = raw.split(",").map { Integer(it.strip) }
usage! if numbers.length < 2
numbers
rescue ArgumentError, NoMethodError
usage!
end
def quicksort!(arr, lo = 0, hi = arr.length - 1)
return arr if lo >= hi
p = partition(arr, lo, hi)
quicksort!(arr, lo, p - 1)
quicksort!(arr, p + 1, hi)
arr
end
def partition(arr, lo, hi)
pivot = arr[hi]
i = lo
(lo...hi).each do |j|
if arr[j] <= pivot
arr[i], arr[j] = arr[j], arr[i]
i += 1
end
end
arr[i], arr[hi] = arr[hi], arr[i]
i
end
numbers = parse_input
puts quicksort!(numbers).join(", ")
Quick 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.