Created
May 9, 2010 15:06
-
-
Save gom/395212 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
# -*- coding: utf-8 -*- | |
def merge ary1, ary2 | |
new_ary = [] | |
until (ary1.empty? || ary2.empty?) | |
if ary1.first < ary2.first | |
new_ary << ary1.shift | |
else | |
new_ary << ary2.shift | |
end | |
end | |
new_ary += ary1 + ary2 | |
end | |
def sort ary | |
return ary if ary.size == 1 | |
point = ary.size / 2 | |
ary_a = sort ary[0...point] | |
ary_b = sort ary[point..-1] | |
merge ary_a, ary_b | |
end | |
a = [8,4,3,7,6,5,2,1] | |
sort a # => [1, 2, 3, 4, 5, 6, 7, 8] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment