 # How do you combine two sorted array algorithms?

Written by Alison Lurie, In , Published On
December 7, 2022
, 77 Views

To store the same kind of data, programming languages use the concept of arrays. This linear data structure is massively used in both real-life applications and programming languages.

Don’t be surprised if you see multiple questions related to arrays in your interviews or exams.

However, if you are practicing the concept of arrays, do not forget to go through a common question, i.e. how to merge two sorted arrays.

## How do you combine two sorted array algorithms?

There is no need to switch between the tabs to understand the problem when we have consolidated everything for you in one place!

Check out everything you need to know about merging two sorted arrays here.

### Understand The Problem Statement

You will be provided with two arrays that are already sorted. You need to write code to merge these two arrays in the sorted order.

For instance, if you are given the following arrays:

• A= { 1, 4, 7,9}
• B= {2, 3, 5, 6}
• The resultant array should look like: C= { 1, 2, 3, 4, 5, 6, 7, 9}

## Methods To Combine Two-Sorted Arrays You can employ four methods to merge two sorted arrays. These include:

• Simple Method
• Merge sorting
• Inserting and sorting method
• Two pointer’s approach

### Simple Method

As the name implies, this is the most basic method to merge two sorted arrays. The procedure consists of the following simple steps:

• To begin, you must select all of the elements from arrays 1 and 2.
• Include all the elements in array 3.
• After that, you must sort array 3.

### Complexity Analysis

• Time Complexity: This method’s time complexity is determined as O(m+n) log(m+n). This is due to the fact that the size of the third array will be m+n.
• Space Complexity: Because we are not using any extra room to complete this process, the space complexity is determined as O. (1).

### Merge Sorting

We will make full use of the fact that both arrays are sorted. To merge two sorted arrays, we will use a similar methodology to merge sort.

To employ this method, simply follow the below steps:

• To begin, you must create an extra space of size n+m.
• You must then use two pointers, j and I and initialise them to 0.
• Now, the pointer I would then point to the first array, and the pointer j will point to the second array.
• You must traverse both arrays concurrently and then select the smallest value present among all elements in both arrays.
• You must add that element into the extra space you have generated.
• Simply keep incrementing the pointers and repeating the process until the arrays are merged.
• Finally, you must return the final merged array.

### Complexity Analysis

• Time Complexity: This method’s time complexity is determined as O(n+m).
• Space Complexity: Since the extra space is required, the space complexity of this method is determined as O(n+m).

### Inserting and Sorting Method

Inserting and sorting an array is another method to merge two sorted arrays. The steps that are taken in this method:

• To begin, create and declare an array A2 of size n1+n2.
• You must now copy all of the elements from A1 to A3.
• You must then traverse Ar2 and begin inserting A3 elements one at a time into Ar1.

### Complexity Analysis

• Time Complexity: This method’s time complexity is determined as O(n1 *n2).
• Space Complexity: Since we are using extra space, the space complexity of this method is determined as O(n1 +n2).

### Two Pointer’s Approach

The most effective approach to merging two sorted arrays is the two-pointer approach.   However, to merge two sorted arrays using a two-pointers approach, you must first understand arrays and how the technique works.

To use the two-pointers approach, go through the steps outlined below:

• To begin, you must initialize the lengths of the given arrays, m, and n.
• Create an extra array of size m+n.
• The loops I k, and j must then be initialized at index 0 for all three arrays.
• If i>mand j>n, begin comparing A[i] and B[j].
• If A[i] is below B[j], you must copy A[i] into the C array. In addition, increase the value of k and i.
• Otherwise, you must copy B[j] into C[k]. Integrate k and j as well.
• Also, if i<m, you must copy A[i] into C[k]. Increase k and i.
• Similarly, if j<n, you must copy B[j] into C[k]. Increase k and j.
• You must repeat these steps till both arrays A and B are vacant.

### Complexity Analysis

• Time Complexity: The time complexity in this method is determined as O(m+n).
• Space Complexity: Since we are not using any additional space, the space complexity is determined as O(1).

Like arrays, the binary tree is also a fundamental data structure of the programming language and one has to face multiple questions related to the binary tree as well.

How to find the lowest common ancestor of a binary search tree is one such problem that is often asked. Let’s take a brief look at what this problem is.

Overview of Lowest Common Ancestor Of A Binary Search Tree Problem

In this problem, you will be given a binary tree and you are required to find a common ancestor for node 1 and node 2 present in the tree. Here, both nodes must be the descendants of the ancestor node.

To resolve this problem, you can use a recursive approach and an iterative approach.

#### Conclusion

Keeping your fundamentals strong is the need of the hour if you wish to thrive in the tech industry. When it comes to programming languages, the array is one of the building blocks that every programmer must be familiar with. There is no doubt that in an interview or an exam, no direct questions are asked.

Therefore, it is important for you to master the implementation of concepts like arrays.

With that being said, how to merge two sorted arrays is one such problem that interviewers ask to analyze your hold on the concept of arrays. Make sure to check out all the methods mentioned above and master the problem before your next interview.

Related articles