Method: RubyLabs::RecursionLab#msort_brackets

Defined in:
lib/recursionlab.rb

#msort_brackets(a, n) ⇒ Object

A method designed to be used in experiments with the merge sort algorithm. A call of the form msort_brackets(a, n) will create a string with every item from a, with brackets around each group of size n.

Example:

>> a = TestArray.new(16)
=> [45, 10, 33, 41, 57, 84, 7, 96, 18, 44, 89, 94, 36, 90, 87, 97]
>> puts msort_brackets(a, 4)
[45 10 33 41] [57 84 7 96] [18 44 89 94] [36 90 87 97]
=> nil

:call-seq:

msort_brackets(a,n) => String


218
219
220
221
222
223
224
225
226
# File 'lib/recursionlab.rb', line 218

def msort_brackets(a, n)
  res = []
  i = 0
  while i < a.length
    res << a[i..((i+n)-1)].join(" ")
    i += n
  end
  return "[" + res.join("] [") + "]"
end