Skip to content

Instantly share code, notes, and snippets.

@raySavignone
Created March 5, 2015 04:41
Show Gist options
  • Save raySavignone/61f304879f26e0ddbe73 to your computer and use it in GitHub Desktop.
Save raySavignone/61f304879f26e0ddbe73 to your computer and use it in GitHub Desktop.
Merge sort
require 'pry'
def separating_logic(arr)
return arr if arr.length == 1
mid_point = arr.length / 2
left_half = arr[0,mid_point]
right_half = arr[mid_point, arr.length]
# binding.pry
sorting_algorithm(separating_logic(left_half),separating_logic(right_half))
end
def sorting_algorithm(left,right)
sorted = []
binding.pry
until left.empty? || right.empty?
if left.first <= right.first
sorted << left.shift
else
sorted << right.shift
end
end
sorted.concat(left).concat(right)
sorted
end
sorted_array = separating_logic([2,4,1,8,5])
puts sorted_array.to_s
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment