Skip to content

Instantly share code, notes, and snippets.

@ackintosh
Last active December 21, 2015 05:49
Show Gist options
  • Save ackintosh/6260083 to your computer and use it in GitHub Desktop.
Save ackintosh/6260083 to your computer and use it in GitHub Desktop.
#gunmacs
require 'pry'
require 'test/unit'
class Array
def bubble_sort
ary = self.dup
ary.size.times do
ary = ary.inject([]) do |result, num|
if result == []
result << num
else
result.last <= num ? result << num : result.insert(0, num)
end
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