Given an integer array arr[], whose elements are divided into two parts around a break point, where both parts are individually sorted in increasing order, merge the two parts to form a single array sorted in increasing order.
Note: The break point can be anywhere in the array, including at the beginning or end.
Examples:
Input: arr[] = [2, 3, 8, -1, 7, 10]
Output: [-1, 2, 3, 7, 8, 10]
Explanation: [2, 3, 8] and [-1, 7, 10] are sorted in the original array. The overall sorted version is [-1 2 3 7 8 10].Input: arr[] = [-4, 6, 9, -1, 3]
Output: [-4, -1, 3, 6, 9]
Explanation: [-4, 6, 9] and [-1, 3] are sorted in the original array. The overall sorted version is [-4 -1 3 6 9].Input: arr[] = [10, 20, 30]
Output: [10, 20, 30]
Explanation: One part is empty and the other part is whole array which is already sorted.
Table of Content
[Naive Approach] Sort Whole Array - O(n Log n) Time
The main logic is to sort the array using built in functions (generally an implementation of Quick Sort).
This approach ignores the “two parts” and simply calls the language’s built‑in sort to sort the entire array.
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void mergeTwoParts(vector<int> &arr)
{
// Sort the given array using sort STL
sort(arr.begin(), arr.end());
}
int main()
{
vector<int> arr = {2, 3, 8, -1, 7, 10};
int n = arr.size();
mergeTwoParts(arr);
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
import java.util.Arrays;
class GFG {
static void mergeTwoParts(int[] arr) {
// Sort the given array using sort STL
Arrays.sort(arr);
}
public static void main(String[] args)
{
int[] arr = { 2, 3, 8, -1, 7, 10 };
mergeTwoParts(arr);
for (int i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
}
}
def mergeTwoParts(arr):
# Sort the given list using list.sort()
arr.sort()
# Driver Code
if __name__ == "__main__":
arr = [2, 3, 8, -1, 7, 10]
n = len(arr)
mergeTwoParts(arr)
for i in range(n):
print(arr[i], end=" ")
using System;
using System.Linq;
class GFG {
static void mergeTwoParts(int[] arr){
// Sort the given array using sort STL
Array.Sort(arr);
}
public static void Main()
{
int[] arr = { 2, 3, 8, -1, 7, 10 };
mergeTwoParts(arr);
for (int i = 0; i < arr.Length; i++)
Console.Write(arr[i] + " ");
}
}
function mergeTwoParts(arr)
{
// Sort the given array using Array.prototype.sort
arr.sort((a, b) => a - b);
}
// Driver Code
const arr = [ 2, 3, 8, -1, 7, 10 ];
const n = arr.length;
mergeTwoParts(arr);
let ans = "";
for (let i = 0; i < n; i++)
ans += arr[i] + " ";
console.log(ans.trim());
Output
-1 2 3 7 8 10
[Expected Approach] Merge of Merge Sort - O(n) Time and O(n) Space
The main idea is to use an auxiliary array which is very similar to the Merge Function of Merge sort.
- Find where the first sorted part ends by checking out of order pair,.
- Walk through both parts side by side, always picking the smaller next number.
#include <iostream>
#include <vector>
using namespace std;
void mergeTwoParts(vector<int> &arr)
{
int n = arr.size();
// starting index of second half
int index = 0;
// Temp Array store sorted resultant array
vector<int> temp(n);
// First Find the point where array is divided
// into two half
for (int i = 0; i < n - 1; i++)
{
if (arr[i] > arr[i + 1])
{
index = i + 1;
break;
}
}
// If Given array is all-ready sorted
if (index == 0)
return;
// Merge two sorted arrays in single sorted array
int i = 0, j = index, k = 0;
while (i < index && j < n)
{
if (arr[i] < arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
}
// Copy the remaining elements of arr[i to index]
while (i < index)
temp[k++] = arr[i++];
// Copy the remaining elements of arr[index to n]
while (j < n)
temp[k++] = arr[j++];
for (int i = 0; i < n; i++)
{
arr[i] = temp[i];
}
}
int main()
{
vector<int> arr = {2, 3, 8, -1, 7, 10};
int n = arr.size();
mergeTwoParts(arr);
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
import java.util.ArrayList;
class GFG {
static void mergeTwoParts(int[] arr)
{
int n = arr.length;
// starting index of second half
int index = 0;
// Temp Array store sorted resultant array
int[] temp = new int[n];
// First Find the point where array is divided
// into two half
for (int i = 0; i < n - 1; i++) {
if (arr[i] > arr[i + 1]) {
index = i + 1;
break;
}
}
// If Given array is all-ready sorted
if (index == 0)
return;
// Merge two sorted arrays in single sorted array
int i = 0, j = index, k = 0;
while (i < index && j < n) {
if (arr[i] < arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
}
// Copy the remaining elements of arr[i to index]
while (i < index)
temp[k++] = arr[i++];
// Copy the remaining elements of arr[index to n]
while (j < n)
temp[k++] = arr[j++];
for (int l = 0; l < n; l++) {
arr[l] = temp[l];
}
}
public static void main(String[] args)
{
int[] arr = { 2, 3, 8, -1, 7, 10 };
int n = arr.length;
mergeTwoParts(arr);
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
}
def mergeTwoParts(arr):
n = len(arr)
# starting index of second half
index = 0
# Temp Array store sorted resultant array
temp = [0] * n
# First Find the point where array is divided
# into two half
for i in range(n - 1):
if arr[i] > arr[i + 1]:
index = i + 1
break
# If Given array is all-ready sorted
if index == 0:
return
# Merge two sorted arrays in single sorted array
i, j, k = 0, index, 0
while i < index and j < n:
if arr[i] < arr[j]:
temp[k] = arr[i]
i += 1
else:
temp[k] = arr[j]
j += 1
k += 1
# Copy the remaining elements of arr[i to index]
while i < index:
temp[k] = arr[i]
i += 1
k += 1
# Copy the remaining elements of arr[index to n]
while j < n:
temp[k] = arr[j]
j += 1
k += 1
for i in range(n):
arr[i] = temp[i]
# Driver Code
if __name__ == "__main__":
arr = [2, 3, 8, -1, 7, 10]
n = len(arr)
mergeTwoParts(arr)
for i in range(n):
print(arr[i], end=" ")
using System;
using System.Collections.Generic;
class GFG {
static void mergeTwoParts(int[] arr)
{
int n = arr.Length;
// starting index of second half
int index = 0;
// Temp Array store sorted resultant array
int[] temp = new int[n];
// First Find the point where array is divided
// into two half
for (int p = 0; p < n - 1; p++) {
if (arr[p] > arr[p + 1]) {
index = p + 1;
break;
}
}
// If Given array is all-ready sorted
if (index == 0)
return;
// Merge two sorted arrays in single sorted array
int i = 0, j = index, k = 0;
while (i < index && j < n) {
if (arr[i] < arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
}
// Copy the remaining elements of arr[i to index]
while (i < index)
temp[k++] = arr[i++];
// Copy the remaining elements of arr[index to n]
while (j < n)
temp[k++] = arr[j++];
for (int l = 0; l < n; l++) {
arr[l] = temp[l];
}
}
public static void Main()
{
int[] arr = { 2, 3, 8, -1, 7, 10 };
int n = arr.Length;
mergeTwoParts(arr);
for (int i = 0; i < n; i++)
Console.Write(arr[i] + " ");
}
}
function mergeTwoParts(arr)
{
let n = arr.length;
// starting index of second half
let index = 0;
// Temp Array store sorted resultant array
let temp = new Array(n);
// First Find the point where array is divided
// into two half
for (let i = 0; i < n - 1; i++) {
if (arr[i] > arr[i + 1]) {
index = i + 1;
break;
}
}
// If Given array is all-ready sorted
if (index === 0)
return;
// Merge two sorted arrays in single sorted array
let i = 0, j = index, k = 0;
while (i < index && j < n) {
if (arr[i] < arr[j])
temp[k++] = arr[i++];
else
temp[k++] = arr[j++];
}
// Copy the remaining elements of arr[i to index]
while (i < index)
temp[k++] = arr[i++];
// Copy the remaining elements of arr[index to n]
while (j < n)
temp[k++] = arr[j++];
for (let i = 0; i < n; i++) {
arr[i] = temp[i];
}
}
// Driver Code
let arr = [ 2, 3, 8, -1, 7, 10 ];
let n = arr.length;
mergeTwoParts(arr);
let ans = "";
for (let i = 0; i < n; i++) {
ans += arr[i] + " ";
}
console.log(ans.trim());
Output
-1 2 3 7 8 10