Skip to content

Instantly share code, notes, and snippets.

@natachaS
Created September 12, 2014 15:33
Show Gist options
  • Save natachaS/17bba4d02f2fb18900ad to your computer and use it in GitHub Desktop.
Save natachaS/17bba4d02f2fb18900ad to your computer and use it in GitHub Desktop.
def merge_sort(m)
return m if m.length <= 1
middle = m.length / 2
left = m[0,middle]
right = m[middle..-1]
left = merge_sort(left)
right = merge_sort(right)
merge(left, right)
end
def merge(left, right)
result = []
until left.empty? || right.empty?
if left.first <= right.first
result << left.shift
else
result << right.shift
end
end
result + left + right
end
ary = [7,6,5,9,8,4,3,1,2,0]
p merge_sort(ary)
# => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment