Created
August 18, 2013 07:04
-
-
Save ackintosh/6260311 to your computer and use it in GitHub Desktop.
#gunmacs InsertionSort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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