Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
29 lines (27 sloc) 721 Bytes
def merge_sort(collection)
if collection.length <= 1
collection
else
mid = (collection.length / 2).floor
left = merge_sort(collection[0..mid - 1])
right = merge_sort(collection[mid..collection.length])
merge(left, right)
end
end
def merge(left, right)
if left.empty?
right
elsif right.empty?
left
elsif left.first < right.first
[left.first] + merge(left[1..left.length], right)
else
[right.first] + merge(left, right[1..right.length])
end
end
=begin
Merge sort is a powerful and efficient sorting algorithm.
Merge sort is frequently used to sort data due to its efficiency
and and relative ease of implementation,
particularly when working with large data sets.
=end
You can’t perform that action at this time.