Skip to content

Instantly share code, notes, and snippets.

@renaehodgkins
Created November 23, 2008 04:18
Show Gist options
  • Save renaehodgkins/28028 to your computer and use it in GitHub Desktop.
Save renaehodgkins/28028 to your computer and use it in GitHub Desktop.
# http://en.wikibooks.org/wiki/Algorithm_implementation/Sorting/Merge_sort
def mergesort(list)
return list if list.size <= 1
mid = list.size / 2
left = list[0, mid]
right = list[mid, list.size]
merge(mergesort(left), mergesort(right))
end
def merge(left, right)
sorted = []
until left.empty? or right.empty?
if left.first <= right.first
sorted << left.shift
else
sorted << 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