a guest Feb 13th, 2018 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- def merge_sort(elements)
- if elements.size <= 1
- return elements
- left = elements[0,(elements.size / 2)]
- right = elements[(elements.size / 2),elements.size]
- sort(merge_sort(left), merge_sort(right))
- def sort(arr1, arr2)
- if arr1.empty?
- elsif arr2.empty?
- elsif arr1 <= arr2
- Array(arr1) + sort(arr1[1..-1], arr2)
- Array(arr2) + sort(arr1, arr2[1..-1])
- elements_to_sort = [10,1,5,3,9,4,2]
- p merge_sort(elements_to_sort) == elements_to_sort.sort
RAW Paste Data