class Algorithmable::Sort::Merge
Public Class Methods
sort(collection)
click to toggle source
# File lib/algorithmable/sort/merge.rb, line 4 def self.sort(collection) return collection if collection.length <= 1 mid = collection.length / 2 left = collection[0...mid] right = collection[mid...collection.length] merge(sort(left), sort(right)) end
Private Class Methods
merge(left, right)
click to toggle source
# File lib/algorithmable/sort/merge.rb, line 14 def self.merge(left, right) sorted = [] until left.empty? || right.empty? left.first <= right.first ? sorted << left.shift : sorted << right.shift end sorted + left + right end