Skip to content

Latest commit

 

History

History
38 lines (29 loc) · 1.86 KB

File metadata and controls

38 lines (29 loc) · 1.86 KB

Algorithm:

  1. Create a new array merged of size m.length + n.length to store the merged sorted array.
  2. Initialize three pointers i, j, and k. i and j will point to the first elements of the arrays m and n, respectively. k will point to the first element of the array merged.
  3. While i is less than the length of m and j is less than the length of n:
    • If the element at i in m is less than or equal to the element at j in n:
      • Copy the element at i in m to the element at k in merged.
      • Increment i and k.
    • Otherwise:
      • Copy the element at j in n to the element at k in merged.
      • Increment j and k.
  4. While i is less than the length of m:
    • Copy the element at i in m to the element at k in merged.
    • Increment i and k.
  5. While j is less than the length of n:
    • Copy the element at j in n to the element at k in merged.
    • Increment j and k.
  6. Calculate the middle index of the array merged.
  7. If the length of the array merged is even:
    • Return the average of the two elements at the middle index and the middle index minus one.
  8. Otherwise:
    • Return the element at the middle index.

Time complexity: O(m + n), where m and n are the lengths of the arrays m and n, respectively. This is because we only need to iterate over the two arrays once.

Space complexity: O(m + n), where m and n are the lengths of the arrays m and n, respectively. This is because we need to store the merged array merged.

for example, consider the following two arrays:

m = [1, 3, 5, 7] n = [2, 4, 6, 8]

The merged array will be:

merged = [1, 2, 3, 4, 5, 6, 7, 8]

The middle index of the merged array is 3. Therefore, the function will return the average of the two elements at index 3 and index 2, which is (3 + 4) / 2 = 3.5.