Created
March 5, 2015 04:41
-
-
Save raySavignone/61f304879f26e0ddbe73 to your computer and use it in GitHub Desktop.
Merge sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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