Module: Tattletail::Merge

Extended by:
Tattletail
Defined in:
lib/tattletail/demo.rb

Constant Summary

Constants included from Tattletail

COLORS, VERSION

Class Method Summary collapse

Methods included from Tattletail

cont_indent_str, context_str, count, count_str, end_indent_str, exception_str, file_str, inc, indent, indent_changed?, indent_level, indent_str, method_str, outdent, remember_indent, reset, result_str, self_str, start_indent_str, tattle_on, tattle_on_class_method, tattle_on_instance_method, time_str

Class Method Details

.merge(left, right) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/tattletail/demo.rb', line 85

def self.merge(left, right)
  sorted = []
  until left.empty? or right.empty?
    if left.first <= right.first
      sorted << left.shift
    else
      sorted << right.shift
    end
  end
  sorted.concat(left).concat(right)
end

.mergesort(list = sample_array) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/tattletail/demo.rb', line 76

def self.mergesort(list = sample_array)
  return list if list.size <= 1
  mid = list.size / 2
  left  = list[0, mid]
  right = list[mid, list.size]
  merge(mergesort(left), mergesort(right))
end

.sample_arrayObject



72
73
74
# File 'lib/tattletail/demo.rb', line 72

def self.sample_array
  [3,7,5,1,2,9,8,0,2,3,5,2,4,1,6,7,8,4,6,2,1]
end