Given two sorted arrays, arr[], brr[] of size N, and M, the task is to merge the two given arrays such that they form a sorted sequence of integers combining elements of both the arrays.
Examples:
Input: arr[] = {10}, brr[] = {2, 3}
Output: 2 3 10
Explanation: The merged sorted array obtained by taking all the elements from the both the arrays is {2, 3, 10}.
Therefore, the required output is 2 3 10.
Input: arr[] = {1, 5, 9, 10, 15, 20}, brr[] = {2, 3, 8, 13}
Output: 1 2 3 5 8 9 10 13 15 20
Naive Approach: Refer to Merge two sorted arrays for the simplest approach to merge the two given arrays.
Time Complexity: O(N * M)
Auxiliary Space: O(1)
Space Optimized Approach: Refer to Merge two sorted arrays with O(1) extra space to merge the two given arrays without using any extra memory.
Time Complexity: O(N * M)
Auxiliary Space: O(1)
Efficient Space Optimized Approach: Refer to Efficiently merging two sorted arrays with O(1) extra space to merge the two given array without using any extra memory.
Time Complexity: O((N + M) * log(N + M))
Auxiliary Space: O(1)
Partition – based Approach: The idea is to consider the (N + 1)th element of the final sorted array as a pivot element and perform the quick sort partition around the pivot element. Finally, store the first N smaller elements of the final sorted array into the array, arr[] and the last M greater elements of the final sorted array into the array, brr[] in any order and sort both these arrays separately. Follow the steps below to solve the problem:
- Initialize a variable, say index to store the index of each element of the final sorted array.
- Find the (N + 1)th element of the final sorted array as a pivot element.
- Perform the quick sort partition around the pivot element.
- Finally, sort both the array arr[] and brr[] separately.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void partition( int arr[], int N,
int brr[], int M,
int Pivot)
{
int l = N - 1;
int r = 0;
while (l >= 0 && r < M) {
if (arr[l] < Pivot)
l--;
else if (brr[r] > Pivot)
r++;
else {
swap(arr[l], brr[r]);
l--;
r++;
}
}
}
void Merge( int arr[], int N,
int brr[], int M)
{
int l = 0;
int r = 0;
int index = -1;
int Pivot = 0;
while (index < N && l < N && r < M) {
if (arr[l] < brr[r]) {
Pivot = arr[l++];
}
else {
Pivot = brr[r++];
}
index++;
}
while (index < N && l < N) {
Pivot = arr[l++];
index++;
}
while (index < N && r < M) {
Pivot = brr[r++];
index++;
}
partition(arr, N, brr,
M, Pivot);
sort(arr, arr + N);
sort(brr, brr + M);
for ( int i = 0; i < N; i++)
cout << arr[i] << " " ;
for ( int i = 0; i < M; i++)
cout << brr[i] << " " ;
}
int main()
{
int arr[] = { 1, 5, 9 };
int brr[] = { 2, 4, 7, 10 };
int N = sizeof (arr) / sizeof (arr[0]);
int M = sizeof (brr) / sizeof (brr[0]);
Merge(arr, N, brr, M);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void partition( int arr[], int N,
int brr[], int M,
int Pivot)
{
int l = N - 1 ;
int r = 0 ;
while (l >= 0 && r < M)
{
if (arr[l] < Pivot)
l--;
else if (brr[r] > Pivot)
r++;
else
{
int t = arr[l];
arr[l] = brr[r];
brr[r] = t;
l--;
r++;
}
}
}
static void Merge( int arr[], int N,
int brr[], int M)
{
int l = 0 ;
int r = 0 ;
int index = - 1 ;
int Pivot = 0 ;
while (index < N && l < N &&
r < M)
{
if (arr[l] < brr[r])
{
Pivot = arr[l++];
}
else
{
Pivot = brr[r++];
}
index++;
}
while (index < N && l < N)
{
Pivot = arr[l++];
index++;
}
while (index < N && r < M)
{
Pivot = brr[r++];
index++;
}
partition(arr, N, brr,
M, Pivot);
Arrays.sort(arr);
Arrays.sort(brr);
for ( int i = 0 ; i < N; i++)
System.out.print(arr[i] + " " );
for ( int i = 0 ; i < M; i++)
System.out.print(brr[i] + " " );
}
public static void main(String[] args)
{
int arr[] = { 1 , 5 , 9 };
int brr[] = { 2 , 4 , 7 , 10 };
int N = arr.length;
int M = brr.length;
Merge(arr, N, brr, M);
}
}
|
Python3
def partition(arr, N, brr, M, Pivot):
l = N - 1
r = 0
while (l > = 0 and r < M):
if (arr[l] < Pivot):
l - = 1
elif (brr[r] > Pivot):
r + = 1
else :
arr[l], brr[r] = brr[r], arr[l]
l - = 1
r + = 1
def Merge(arr, N, brr, M):
l = 0
r = 0
index = - 1
Pivot = 0
while (index < N and l < N and r < M):
if (arr[l] < brr[r]):
Pivot = arr[l]
l + = 1
else :
Pivot = brr[r]
r + = 1
index + = 1
while (index < N and l < N):
Pivot = arr[l]
l + = 1
index + = 1
while (index < N and r < M):
Pivot = brr[r]
r + = 1
index + = 1
partition(arr, N, brr, M, Pivot)
arr = sorted (arr)
brr = sorted (brr)
for i in range (N):
print (arr[i], end = " " )
for i in range (M):
print (brr[i], end = " " )
if __name__ = = '__main__' :
arr = [ 1 , 5 , 9 ]
brr = [ 2 , 4 , 7 , 10 ]
N = len (arr)
M = len (brr)
Merge(arr, N, brr, M)
|
C#
using System;
class GFG{
static void partition( int []arr, int N,
int []brr, int M,
int Pivot)
{
int l = N - 1;
int r = 0;
while (l >= 0 && r < M)
{
if (arr[l] < Pivot)
l--;
else if (brr[r] > Pivot)
r++;
else
{
int t = arr[l];
arr[l] = brr[r];
brr[r] = t;
l--;
r++;
}
}
}
static void Merge( int []arr, int N,
int []brr, int M)
{
int l = 0;
int r = 0;
int index = -1;
int Pivot = 0;
while (index < N && l < N &&
r < M)
{
if (arr[l] < brr[r])
{
Pivot = arr[l++];
}
else
{
Pivot = brr[r++];
}
index++;
}
while (index < N && l < N)
{
Pivot = arr[l++];
index++;
}
while (index < N && r < M)
{
Pivot = brr[r++];
index++;
}
partition(arr, N, brr,
M, Pivot);
Array.Sort(arr);
Array.Sort(brr);
for ( int i = 0; i < N; i++)
Console.Write(arr[i] + " " );
for ( int i = 0; i < M; i++)
Console.Write(brr[i] + " " );
}
public static void Main(String[] args)
{
int []arr = {1, 5, 9};
int []brr= {2, 4, 7, 10};
int N = arr.Length;
int M = brr.Length;
Merge(arr, N, brr, M);
}
}
|
Javascript
function partition( arr, N,
brr, M,
Pivot)
{
let l = N - 1;
let r = 0;
while (l >= 0 && r < M) {
if (arr[l] < Pivot)
l--;
else if (brr[r] > Pivot)
r++;
else {
[arr[l],brr[r]]=[brr[r],arr[l]];
l--;
r++;
}
}
}
function Merge(arr, N,
brr, M)
{
let l = 0;
let r = 0;
let index = -1;
let Pivot = 0;
while (index < N && l < N && r < M) {
if (arr[l] < brr[r]) {
Pivot = arr[l++];
}
else {
Pivot = brr[r++];
}
index++;
}
while (index < N && l < N) {
Pivot = arr[l++];
index++;
}
while (index < N && r < M) {
Pivot = brr[r++];
index++;
}
partition(arr, N, brr,
M, Pivot);
arr.sort((a,b)=>a-b);
brr.sort((a,b)=>a-b);
for (let i = 0; i < N; i++)
console.log(arr[i]);
for (let i = 0; i < M; i++)
console.log(brr[i]);
}
let arr = [1, 5, 9 ];
let brr = [2, 4, 7, 10 ];
let N = arr.length;
let M = brr.length;
Merge(arr, N, brr, M);
|
Time Complexity: O((N + M)log(N + M))
Auxiliary Space: O(1)
Efficient Approach: Refer to merge two sorted arrays to efficiently merge the two given arrays.
Time Complexity: O(N + M)
Auxiliary Space: O(N + M)