Ruby-chan

Ruby code posted by Me
created at 05 Dec 08:27

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Array
  def merge_sort
    return self if self.length <= 1

    middle = (self.length / 2).to_i
    left = self[0..middle-1].merge_sort
    right = self[middle..self.length-1].merge_sort

    return merge(left, right)
  end

  def merge(left, right)
    result = Array.new

    while left.length > 0 || right.length > 0
      if left.length > 0 && right.length > 0
        if left[0] <= right[0]
          result << left.slice!(0)
        else
          result << right.slice!(0)
        end
      elsif left.length > 0
        result.concat left.slice!(0..left.length-1)
      elsif right.length > 0
        result.concat right.slice!(0..right.length-1)
      end
    end

    return result
  end
end

random_names = [1, 45, 87, 14, 2, 89, 111]
puts random_names.merge_sort
813 Bytes in 3 ms with coderay