LeetCode: Merge Sorted Array

Question

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

Note:

The number of elements initialized in nums1 and nums2 are m and n respectively. You may assume that nums1 has enough space (size that is equal to m + n) to hold additional elements from nums2.

Example:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]

Solution #1

def merge(nums1, m, nums2, n)
  while i < m + n
    nums1[i + m] = nums2[i] if nums2[i]
    i += 1
  end
  bubble_sort(nums1)
end

def bubble_sort(nums)
  j = 0
  i = 0
  while j < nums.length - 1
    while i < nums.length - 1
      nums[i], nums[i + 1] = nums[i + 1], nums[i] if nums[i + 1] && nums[i] > nums[i + 1]
      i += 1
    end
    i = 0
    j += 1
  end
  nums
end