Project

array-sort

0.0
No commit activity in last 3 years
No release in over 3 years
Implements sorting algorithms such as merge sort, bubble sort, heap sort, etc. for Arrays.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 1.16
~> 5.0
~> 10.0
 Project Readme

ArraySort

Gem Version

ArraySort adds methods in the Ruby Array class that provides sorting methods using popular sorting algorithms.

Currently, the following sorting algorithms are implemented:

  • Bubble sort (stable)
  • Heap sort (unstable)
  • Insertion sort (stable)
  • Merge sort (stable)
  • Quicksort (unstable)

Note that this gem does not overwrite Array#sort, Array#sort!, Array#sort_by, or Array#sort_by!. Calling those methods will invoke the native sorting methods, which use in-place quicksort algorithm and are unstable.

Installation

Add this line to your application's Gemfile:

gem 'array-sort'

And then execute:

$ bundle

Or install it yourself as:

$ gem install array-sort

Usage

All sort methods share the same signature of Array#sort, Array#sort!, Array#sort_by and Array#sort_by!. Simply change the names of the sorting methods to the following corresponding methods of a specific sorting algorithm:

  • Bubble sort: bubble_sort, bubble_sort!, bubble_sort_by, bubble_sort_by!
  • Heap sort: heap_sort, heap_sort!, heap_sort_by, heap_sort_by!
  • Insertion sort: insertion_sort, insertion_sort!, insertion_sort_by, insertion_sort_by!
  • Merge sort: merge_sort, merge_sort!, merge_sort_by, merge_sort_by!
  • Quicksort: quick_sort, quick_sort!, quick_sort_by, quick_sort_by!

See the official Ruby documentation on how to use the native sorting methods of Array.

For example, to sort an array using insertion sort:

[3, 1, 2].insertion_sort # => [1, 2, 3]

# The following two lines both produce [[1, 4], [2, 6], [3, 3]]
[[3, 3], [1, 4], [2, 6]].insertion_sort { |a, b| a[0] <=> b[0] }
[[3, 3], [1, 4], [2, 6]].insertion_sort_by { |e| e[0] }

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/foxhatleo/array-sort. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.