Created
August 18, 2013 06:26
-
-
Save ackintosh/6260199 to your computer and use it in GitHub Desktop.
#gunmacs BubbleSort
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 bubble_sort | |
ary = self.dup | |
(size - 1).downto(0) do |i| | |
(0...i).each 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_bubble_sort | |
assert_equal([], [].bubble_sort) | |
assert_equal([1], [1].bubble_sort) | |
assert_equal([1, 2], [2, 1].bubble_sort) | |
assert_equal([1, 2, 3, 4], [2, 4, 3, 1].bubble_sort) | |
assert_equal([1, 2, 3, 4, 5], [5, 2, 4, 3, 1].bubble_sort) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment