daily pastebin goal
36%
SHARE
TWEET

Untitled

a guest Feb 13th, 2018 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def merge_sort(elements)
  2.   if elements.size <= 1
  3.     return elements
  4.   else
  5.     left = elements[0,(elements.size / 2)]
  6.     right = elements[(elements.size / 2),elements.size]
  7.     sort(merge_sort(left), merge_sort(right))
  8.   end
  9. end
  10.  
  11. def sort(arr1, arr2)
  12.   if arr1.empty?
  13.     arr2
  14.   elsif arr2.empty?
  15.     arr1
  16.   elsif arr1[0] <= arr2[0]
  17.     Array(arr1[0]) + sort(arr1[1..-1], arr2)
  18.   else
  19.     Array(arr2[0]) + sort(arr1, arr2[1..-1])
  20.   end
  21. end
  22.  
  23. elements_to_sort = [10,1,5,3,9,4,2]
  24. p merge_sort(elements_to_sort) == elements_to_sort.sort
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top