Merge two sorted arrays (original) (raw)
Last Updated : 28 Feb, 2025
Given two sorted arrays, the task is to merge them in a sorted manner.
**Examples:
**Input: arr1[] = { 1, 3, 4, 5}, arr2[] = {2, 4, 6, 8}
**Output: arr3[] = {1, 2, 3, 4, 4, 5, 6, 8}**Input: arr1[] = { 5, 8, 9}, arr2[] = {4, 7, 8}
**Output: arr3[] = {4, 5, 7, 8, 8, 9}
Table of Content
- [Naive Approach] Concatenate and Sort - O((n1 + n2) log(n1 + n2)) time and O(1) space
- [Expected Approach] Using Merge of Merge Sort O(n1 + n2) time and O(n1 + n2) space
- Related Articles :
[Naive Approach] Concatenate and Sort - O((n1 + n2) log(n1 + n2)) time and O(1) space
This approach involves two key steps: first, combining elements from two separate arrays into a third result array, and then sorting the result array.
Since it requires sorting the entire merged array, resulting in a time complexity of O((n1 + n2) log(n1 + n2)), where n is the total number of elements.
C++ `
#include <bits/stdc++.h> using namespace std;
void mergeArrays(vector& arr1, vector& arr2, vector& arr3) { int n1 = arr1.size(); int n2 = arr2.size(); int i = 0, j = 0, k = 0;
// Traverse arr1 and insert its elements into arr3
while (i < n1) {
arr3[k++] = arr1[i++];
}
// Traverse arr2 and insert its elements into arr3
while (j < n2) {
arr3[k++] = arr2[j++];
}
// Sort the entire arr3
sort(arr3.begin(), arr3.end());
}
// Driver code int main() { vector arr1 = {1, 3, 5, 7}; vector arr2 = {2, 4, 6, 8}; vector arr3(arr1.size() + arr2.size());
mergeArrays(arr1, arr2, arr3);
for (int i = 0; i < arr3.size(); i++)
cout << arr3[i] << " ";
return 0;
}
C
#include <stdio.h> #include <stdlib.h>
// Function to merge two arrays and sort the result void mergeArrays(int* arr1, int n1, int* arr2, int n2, int* arr3) { int i = 0, j = 0, k = 0;
// Traverse arr1 and insert its elements into arr3
while (i < n1) {
arr3[k++] = arr1[i++];
}
// Traverse arr2 and insert its elements into arr3
while (j < n2) {
arr3[k++] = arr2[j++];
}
// Sort the entire arr3
qsort(arr3, n1 + n2, sizeof(int), [](const void* a, const void* b) {
return (*(int*)a - *(int*)b);
});
}
// Driver code int main() { int arr1[] = {1, 3, 5, 7}; int arr2[] = {2, 4, 6, 8}; int n1 = sizeof(arr1) / sizeof(arr1[0]); int n2 = sizeof(arr2) / sizeof(arr2[0]); int arr3[n1 + n2];
mergeArrays(arr1, n1, arr2, n2, arr3);
for (int i = 0; i < n1 + n2; i++)
printf("%d ", arr3[i]);
return 0;
}
Java
import java.util.Arrays;
public class GfG {
// Function to merge two arrays and sort the result
public static void mergeArrays(int[] arr1, int[] arr2, int[] arr3) {
int n1 = arr1.length;
int n2 = arr2.length;
int i = 0, j = 0, k = 0;
// Traverse arr1 and insert its elements into arr3
while (i < n1) {
arr3[k++] = arr1[i++];
}
// Traverse arr2 and insert its elements into arr3
while (j < n2) {
arr3[k++] = arr2[j++];
}
// Sort the entire arr3
Arrays.sort(arr3);
}
// Driver code
public static void main(String[] args) {
int[] arr1 = {1, 3, 5, 7};
int[] arr2 = {2, 4, 6, 8};
int[] arr3 = new int[arr1.length + arr2.length];
mergeArrays(arr1, arr2, arr3);
for (int value : arr3)
System.out.print(value + " ");
}
}
Python
Function to merge two arrays and sort the result
def merge_arrays(arr1, arr2): arr3 = arr1 + arr2 arr3.sort() return arr3
Driver code
arr1 = [1, 3, 5, 7] arr2 = [2, 4, 6, 8] arr3 = merge_arrays(arr1, arr2)
for value in arr3: print(value, end=" ")
C#
using System; using System.Linq;
public class GfG {
// Function to merge two arrays and sort the result
public static int[] MergeArrays(int[] arr1, int[] arr2) {
int[] arr3 = arr1.Concat(arr2).ToArray();
Array.Sort(arr3);
return arr3;
}
// Driver code
public static void Main() {
int[] arr1 = {1, 3, 5, 7};
int[] arr2 = {2, 4, 6, 8};
int[] arr3 = MergeArrays(arr1, arr2);
foreach (int value in arr3)
Console.Write(value + " ");
}
}
JavaScript
// Function to merge two arrays and sort the result function mergeArrays(arr1, arr2) { let arr3 = arr1.concat(arr2); arr3.sort((a, b) => a - b); return arr3; }
// Driver code let arr1 = [1, 3, 5, 7]; let arr2 = [2, 4, 6, 8]; let arr3 = mergeArrays(arr1, arr2);
console.log(arr3.join(" "));
`
[Expected Approach] Using Merge of Merge Sort O(n1 + n2) time and O(n1 + n2) space
The idea is to use Merge function of Merge sort.
- Create an array arr3[] of size n1 + n2.
- Simultaneously traverse arr1[] and arr2[].
- Pick smaller of current elements in arr1[] and arr2[], copy this smaller element to next position in arr3[] and move ahead in arr3[] and the array whose element is picked.
- If there are remaining elements in arr1[] or arr2[], copy them also in arr3[].
C++ `
#include #include using namespace std;
// Merge ar1[0..n1-1] and ar2[0..n2-1] into ar3 void mergeArrays(vector& ar1, vector& ar2, vector& ar3) { int i = 0, j = 0, k = 0; int n1 = ar1.size(); int n2 = ar2.size();
while (i < n1 && j < n2) {
// Pick smaller of the two current
// elements and move ahead in the
// array of the picked element
if (ar1[i] < ar2[j])
ar3[k++] = ar1[i++];
else
ar3[k++] = ar2[j++];
}
// if there are remaining elements of
// the first array, move them
while (i < n1)
ar3[k++] = ar1[i++];
// Else if there are remaining elements of
// the second array, move them
while (j < n2)
ar3[k++] = ar2[j++];
}
// Driver code int main() { vector ar1 = {1, 3, 5, 7}; vector ar2 = {2, 4, 6, 8}; vector ar3(ar1.size() + ar2.size());
mergeArrays(ar1, ar2, ar3);
for (int i = 0; i < ar3.size(); i++)
cout << ar3[i] << " ";
return 0;
}
C
#include <stdio.h>
// Merge ar1[0..n1-1] and ar2[0..n2-1] into ar3 void mergeArrays(int* ar1, int n1, int* ar2, int n2, int* ar3) { int i = 0, j = 0, k = 0;
while (i < n1 && j < n2) {
// Pick smaller of the two current elements and move ahead in the array of the picked element
if (ar1[i] < ar2[j])
ar3[k++] = ar1[i++];
else
ar3[k++] = ar2[j++];
}
// if there are remaining elements of the first array, move them
while (i < n1)
ar3[k++] = ar1[i++];
// Else if there are remaining elements of the second array, move them
while (j < n2)
ar3[k++] = ar2[j++];
}
// Driver code int main() { int ar1[] = {1, 3, 5, 7}; int ar2[] = {2, 4, 6, 8}; int n1 = sizeof(ar1) / sizeof(ar1[0]); int n2 = sizeof(ar2) / sizeof(ar2[0]); int ar3[n1 + n2];
mergeArrays(ar1, n1, ar2, n2, ar3);
for (int i = 0; i < n1 + n2; i++)
printf("%d ", ar3[i]);
return 0;
}
Java
import java.util.*;
public class GfG {
// Merge ar1[0..n1-1] and ar2[0..n2-1] into ar3
public static void mergeArrays(int[] ar1, int[] ar2, int[] ar3) {
int i = 0, j = 0, k = 0;
int n1 = ar1.length;
int n2 = ar2.length;
while (i < n1 && j < n2) {
// Pick smaller of the two current elements and move ahead in the array of the picked element
if (ar1[i] < ar2[j])
ar3[k++] = ar1[i++];
else
ar3[k++] = ar2[j++];
}
// if there are remaining elements of the first array, move them
while (i < n1)
ar3[k++] = ar1[i++];
// Else if there are remaining elements of the second array, move them
while (j < n2)
ar3[k++] = ar2[j++];
}
// Driver code
public static void main(String[] args) {
int[] ar1 = {1, 3, 5, 7};
int[] ar2 = {2, 4, 6, 8};
int[] ar3 = new int[ar1.length + ar2.length];
mergeArrays(ar1, ar2, ar3);
for (int value : ar3)
System.out.print(value + " ");
}
}
Python
Merge ar1[0..n1-1] and ar2[0..n2-1] into ar3
def merge_arrays(ar1, ar2): i = 0 j = 0 k = 0 n1 = len(ar1) n2 = len(ar2) ar3 = []
while i < n1 and j < n2:
# Pick smaller of the two current elements and move ahead in the array of the picked element
if ar1[i] < ar2[j]:
ar3.append(ar1[i])
i += 1
else:
ar3.append(ar2[j])
j += 1
# if there are remaining elements of the first array, move them
while i < n1:
ar3.append(ar1[i])
i += 1
# Else if there are remaining elements of the second array, move them
while j < n2:
ar3.append(ar2[j])
j += 1
return ar3
Driver code
ar1 = [1, 3, 5, 7] ar2 = [2, 4, 6, 8] ar3 = merge_arrays(ar1, ar2)
print(" ".join(map(str, ar3)))
C#
using System;
public class GfG {
// Merge ar1[0..n1-1] and ar2[0..n2-1] into ar3
public static void MergeArrays(int[] ar1, int[] ar2, int[] ar3) {
int i = 0, j = 0, k = 0;
int n1 = ar1.Length;
int n2 = ar2.Length;
while (i < n1 && j < n2) {
// Pick smaller of the two current elements and move ahead in the array of the picked element
if (ar1[i] < ar2[j])
ar3[k++] = ar1[i++];
else
ar3[k++] = ar2[j++];
}
// if there are remaining elements of the first array, move them
while (i < n1)
ar3[k++] = ar1[i++];
// Else if there are remaining elements of the second array, move them
while (j < n2)
ar3[k++] = ar2[j++];
}
// Driver code
public static void Main() {
int[] ar1 = {1, 3, 5, 7};
int[] ar2 = {2, 4, 6, 8};
int[] ar3 = new int[ar1.Length + ar2.Length];
MergeArrays(ar1, ar2, ar3);
foreach (int value in ar3)
Console.Write(value + " ");
}
}
JavaScript
// Merge ar1[0..n1-1] and ar2[0..n2-1] into ar3 function mergeArrays(ar1, ar2) { let i = 0, j = 0, k = 0; const n1 = ar1.length; const n2 = ar2.length; const ar3 = [];
while (i < n1 && j < n2) {
// Pick smaller of the two current elements and move ahead in the array of the picked element
if (ar1[i] < ar2[j])
ar3[k++] = ar1[i++];
else
ar3[k++] = ar2[j++];
}
// if there are remaining elements of the first array, move them
while (i < n1)
ar3[k++] = ar1[i++];
// Else if there are remaining elements of the second array, move them
while (j < n2)
ar3[k++] = ar2[j++];
return ar3;
}
// Driver code const ar1 = [1, 3, 5, 7]; const ar2 = [2, 4, 6, 8]; const ar3 = mergeArrays(ar1, ar2);
console.log(ar3.join(" "));
`
**Problems related to Merging Two Sorted Arrays
Merge two sorted arrays with O(1) extra space
Merge k sorted arrays
Union of Two Sorted Arrays
Intersection of Two Sorted Arrays Merge 2 sorted linked list in reverse order Sort last M elements Merge 2 sorted linked list in reverse order Merge Sort for Doubly Linked List