Top Level Namespace

Instance Method Summary collapse

Instance Method Details

#merge(nums1, m, nums2, n) ⇒ Void

Returns Do not return anything, modify nums1 in-place instead.

Parameters:

  • nums1 (Integer[])
  • m (Integer)
  • nums2 (Integer[])
  • n (Integer)

Returns:

  • (Void)

    Do not return anything, modify nums1 in-place instead.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/week1/merge_sorted_array.rb', line 8

def merge(nums1, m, nums2, n)
  i = m - 1 # Pointer for the last valid element in nums1
  j = n - 1 # Pointer for the last element in nums2
  k = m + n - 1 # Pointer for the last position in nums1

  # Merge the arrays from the end to the beginning
  while i >= 0 && j >= 0
    if nums1[i] > nums2[j]
      nums1[k] = nums1[i]
      i -= 1
    else
      nums1[k] = nums2[j]
      j -= 1
    end
    k -= 1
  end

  # If there are any elements left in nums2, copy them
  while j >= 0
    nums1[k] = nums2[j]
    j -= 1
    k -= 1
  end
end