Skip to content

Instantly share code, notes, and snippets.

@ninjapanzer
Last active December 11, 2015 23:18
Show Gist options
  • Save ninjapanzer/4675725 to your computer and use it in GitHub Desktop.
Save ninjapanzer/4675725 to your computer and use it in GitHub Desktop.
Merge Sort with so Debugging Statements I pulled this from the web somewhere but I don't remember the URL and made some modifications
a = [5,4,1,8,7,2,6,3,5,2,3,45,1,1,3,4,99,2,3]
puts a.size
@recurse = 0
def mergesort(list)
return list if list.size <= 1
mid = list.size / 2
left = list[0, mid]
right = list[mid, list.size]
@recurse = @recurse + 1
puts @recurse
merge(mergesort(left), mergesort(right))
end
def merge(left, right)
sorted = []
until left.empty? or right.empty?
if left.first <= right.first
#puts "inversion"
sorted << left.shift
else
#puts "inversion"
sorted << right.shift
end
end
sorted.concat(left).concat(right)
end
puts mergesort a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment