Skip to content

Instantly share code, notes, and snippets.

@farnoy
Created March 18, 2012 12:47
Show Gist options
  • Select an option

  • Save farnoy/2071631 to your computer and use it in GitHub Desktop.

Select an option

Save farnoy/2071631 to your computer and use it in GitHub Desktop.
def mergesort(list)
return list if list.size <= 1
mid = list.size / 2
merge mergesort(list[0, mid]), mergesort(list[mid, list.size])
end
def merge(left, right)
sorted = []
until left.empty? or right.empty?
if left.first <= right.first
sorted.push left.shift
else
sorted.push right.shift
end
end
sorted.concat(left).concat(right)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment