Given two sorted arraysarr[]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:

  1. Initialize a variable, say index to store the index of each element of the final sorted array.
  2. Find the (N + 1)th elementof the final sorted array as a pivot element.
  3. Perform the quick sort partition around the pivot element.
  4. Finally, sort both the array arr[] and brr[] separately.

Below is the implementation of the above approach:

  • C++

// C++ program to implement

// the above approach

#include <bits/stdc++.h>

**using** **namespace** std;

// Function to perform the partition

// around the pivot element

**void** partition(``**int** arr[], **int** N,

**int** brr[], **int** M,

**int** Pivot)

{

// Stores index of each element

// of the array, arr[]

**int** l = N - 1;

// Stores index of each element

// of the array, brr[]

**int** r = 0;

// Traverse both the array

**while** (l >= 0 && r < M) {

// If pivot is

// smaller than arr[l]

**if** (arr[l] < Pivot)

l--;

// If Pivot is

// greater than brr[r]

**else** **if** (brr[r] > Pivot)

r++;

// If either arr[l] > Pivot

// or brr[r] < Pivot

**else** {

swap(arr[l], brr[r]);

l--;

r++;

}

}

}

// Function to merge

// the two sorted array

**void** Merge(``**int** arr[], **int** N,

**int** brr[], **int** M)

{

// Stores index of each element

// of the array arr[]

**int** l = 0;

// Stores index of each element

// of the array brr[]

**int** r = 0;

// Stores index of each element

// the final sorted array

**int** index = -1;

// Stores the pivot element

**int** Pivot = 0;

// Traverse both the array

**while** (index < N && l < N && r < M) {

**if** (arr[l] < brr[r]) {

Pivot = arr[l++];

}

**else** {

Pivot = brr[r++];

}

index++;

}

// If pivot element is not found

// or index < N

**while** (index < N && l < N) {

Pivot = arr[l++];

index++;

}

// If pivot element is not found

// or index < N

**while** (index < N && r < M) {

Pivot = brr[r++];

index++;

}

// Place the first N elements of

// the sorted array into arr[]

// and the last M elements of

// the sorted array into brr[]

partition(arr, N, brr,

M, Pivot);

// Sort both the arrays

sort(arr, arr + N);

sort(brr, brr + M);

// Print the first N elements

// in sorted order

**for** (``**int** i = 0; i < N; i++)

cout << arr[i] << " "``;

// Print the last M elements

// in sorted order

**for** (``**int** i = 0; i < M; i++)

cout << brr[i] << " "``;

}

// Driver Code

**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;

}

Output:

1 2 4 5 7 9 10

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)_

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

#arrays #divide and conquer #mathematical #sorting #amazon #amdocs #array-merge #brocade #goldman sachs #juniper networks #linkedin #partition #quick sort #snapdeal

Merge two sorted arrays in O(1) extra space using QuickSort partition
7.25 GEEK