Skip to content

Instantly share code, notes, and snippets.

@micahboyd
Created September 13, 2019 01:12
Show Gist options
  • Select an option

  • Save micahboyd/b6204d83f67cca47e5e80ac62ab98366 to your computer and use it in GitHub Desktop.

Select an option

Save micahboyd/b6204d83f67cca47e5e80ac62ab98366 to your computer and use it in GitHub Desktop.
Ruby Merge Sort
def merge_sort(array)
return array if array.length <= 1
middle = array.length / 2
left = merge_sort(array[0...middle])
right = merge_sort(array[middle..])
merge(left, right)
end
def merge(left, right)
return left if right.empty?
return right if left.empty?
if left.first < right.first
[left.first] + merge(left[1..], right)
else
[right.first] + merge(left, right[1..])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment