Skip to content

Instantly share code, notes, and snippets.

@ackintosh
Created August 18, 2013 06:26
Show Gist options
  • Save ackintosh/6260199 to your computer and use it in GitHub Desktop.
Save ackintosh/6260199 to your computer and use it in GitHub Desktop.
#gunmacs BubbleSort
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