Module: Algorithmable::Cups

Defined in:
lib/algorithmable/cups.rb,
lib/algorithmable/cups/stocks.rb,
lib/algorithmable/cups/two_sum.rb,
lib/algorithmable/cups/primitives.rb,
lib/algorithmable/cups/root_cube_issue.rb,
lib/algorithmable/cups/merge_two_arrays.rb,
lib/algorithmable/cups/stacks_and_queues.rb,
lib/algorithmable/cups/nested_lists_problem.rb,
lib/algorithmable/cups/circular_dependencies.rb,
lib/algorithmable/cups/longest_common_subsequence.rb,
lib/algorithmable/cups/task_shedule_with_coldtime.rb,
lib/algorithmable/cups/number_of_occurrences_in_array.rb,
lib/algorithmable/cups/stacks_and_queues/stack_sorter.rb,
lib/algorithmable/cups/stacks_and_queues/triple_stack.rb,
lib/algorithmable/cups/stacks_and_queues/stack_with_min.rb,
lib/algorithmable/cups/stacks_and_queues/towers_of_hanoi.rb,
lib/algorithmable/cups/stacks_and_queues/two_stacks_queue.rb

Defined Under Namespace

Modules: CircularDependencies, NestedListsProblem, NumberOfOccurrencesInArray, Primitives, RootCubeIssue, StacksAndQueues, Stocks, TwoSum Classes: LongestCommonSubSequence, TaskScheduleWithColdTime

Instance Method Summary collapse

Instance Method Details

#merge_arrays(left, right) ⇒ Object

right = [4, 2, 1] left = [7, 6, 5, 3]

l1 = [1] l2 = []


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/algorithmable/cups/merge_two_arrays.rb', line 15

def merge_arrays(left, right)
  sorted = []

  while !left.empty? && !right.empty?
    if left[0] >= right[0]
      sorted.push(left.shift)
    else
      sorted.push(right.shift)
    end
  end

  sorted += left if right.empty?
  sorted += right if left.empty?
  sorted # [1]
end