Skip to content

Instantly share code, notes, and snippets.

@ackintosh
Created August 18, 2013 07:04
Show Gist options
  • Save ackintosh/6260311 to your computer and use it in GitHub Desktop.
Save ackintosh/6260311 to your computer and use it in GitHub Desktop.
#gunmacs InsertionSort
require 'pry'
require 'test/unit'
class Array
def insertion_sort
ary = self.dup
(1...size).each do |i|
(i).downto(1) do |j|
ary[j], ary[j - 1] = ary[j - 1], ary[j] if ary[j] < ary[j - 1]
end
end
ary
end
end
class ArrayTest < Test::Unit::TestCase
def test_insertion_sort
assert_equal([], [].insertion_sort)
assert_equal([1], [1].insertion_sort)
assert_equal([1, 2], [2, 1].insertion_sort)
assert_equal([1, 2, 3, 4], [2, 4, 3, 1].insertion_sort)
assert_equal([1, 2, 3, 4, 5], [5, 2, 4, 3, 1].insertion_sort)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment