Sort an array when two halves are sorted
Last Updated :
23 Jul, 2025
Given an integer array arr[] whose first k elements and remaining n–k elements (for any 0 ≤ k ≤ n) are each sorted, merge these two sorted halves into a single fully sorted array.
Note : There doesn't exists more than two sorted halves in an array.
Examples:
Input : arr[] = [2, 3, 8, -1, 7, 10]
Output : [-1, 2, 3, 7, 8, 10]
Explanation : First and last three sorted elements are [2, 3, 8] and [-1, 7, 10], after merging both halves we get a final sorted array.
Input : arr[] = [-4, 6, 9, -1, 3]
Output : [-4, -1, 3, 6, 9]
Explanation : First three and last two sorted elements are [-4, 6, 9] and [-1, 3], after merging both halves we get a final sorted array.
[Naive Approach] Sort the array
The main logic is to sort the array using built in functions (generally an implementation of quick sort). This approach ignores the “two halves” and simply calls the language’s built‑in sort to sort the entire array.
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> mergeHalves(vector<int> &arr){
// Sort the given array using sort STL
sort(arr.begin(), arr.end());
return arr;
}
int main(){
vector<int> arr = {2, 3, 8, -1, 7, 10};
int n = arr.size();
mergeHalves(arr);
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
Java
import java.util.ArrayList;
import java.util.Arrays;
public class GFG {
public static ArrayList<Integer> mergeHalves(int[] arr) {
// Sort the given array using Arrays.sort
Arrays.sort(arr);
//Build and return an ArrayList from the sorted array
ArrayList<Integer> result = new ArrayList<>(arr.length);
for (int x : arr) {
result.add(x);
}
return result;
}
public static void main(String[] args) {
int[] arr = {2, 3, 8, -1, 7, 10};
int n = arr.length;
ArrayList<Integer> ans = mergeHalves(arr);
for (int i = 0; i < n; i++) {
System.out.print(ans.get(i) + " ");
}
}
}
Python
def mergeHalves(arr):
# Sort the given list using list.sort()
arr.sort()
return arr
if __name__ == "__main__":
arr = [2, 3, 8, -1, 7, 10]
n = len(arr)
mergeHalves(arr)
for i in range(n):
print(arr[i], end=" ")
C#
using System;
using System.Collections.Generic;
class GFG {
static List<int> mergeHalves(int[] arr) {
// Sort the given array using Array.Sort()
Array.Sort(arr);
List<int> result = new List<int>(arr.Length);
foreach (int x in arr) {
result.Add(x);
}
return result;
}
static void Main() {
int[] arr = { 2, 3, 8, -1, 7, 10 };
int n = arr.Length;
List<int> ans = mergeHalves(arr);
for (int i = 0; i < n; i++)
Console.Write(ans[i] + " ");
}
}
JavaScript
function mergeHalves(arr) {
// Sort the given array using Array.prototype.sort
arr.sort((a, b) => a - b);
return arr;
}
// Driver Code
const arr = [2, 3, 8, -1, 7, 10];
const n = arr.length;
mergeHalves(arr);
for (let i = 0; i < n; i++)
process.stdout.write(arr[i] + " ");
PHP
<?php
function mergeHalves(&$arr) {
// Sort the given array using sort()
sort($arr);
return $arr;
}
$arr = [2, 3, 8, -1, 7, 10];
$n = count($arr);
mergeHalves($arr);
for ($i = 0; $i < $n; $i++) {
echo $arr[$i] . " ";
}
Time Complexity : O(n(log(n)))
Auxiliary Space : O(log(n))
Note :- For Java, Python and C# the Space Complexity will be O(n)
[Expected Approach] Using merge sort - O(n) Time and O(n) Space
The main idea behind this approachis to use an auxiliary array which is very similar to the Merge Function of Merge sort. Find where the first sorted part ends, then walk through both parts side by side, always picking the smaller next number to build the fully sorted list.
C++
#include <iostream>
#include <vector>
using namespace std;
vector<int> mergeHalves(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 arr;
// 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++];
return temp;
}
int main(){
vector<int> arr = {2, 3, 8, -1, 7, 10};
int n = arr.size();
vector<int> ans(n);
ans = mergeHalves(arr);
for (int i = 0; i < n; i++)
cout << ans[i] << " ";
return 0;
}
Java
import java.util.ArrayList;
class GFG {
public static ArrayList<Integer> mergeHalves(int[] arr) {
int n = arr.length;
// starting index of second half
int index= 0;
// Temp Array store sorted resultant array
ArrayList<Integer> temp = new ArrayList<>();
for (int i = 0; i < n; i++) temp.add(0);
// 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) {
ArrayList<Integer> original = new ArrayList<>();
for (int x : arr) original.add(x);
return original;
}
// 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.set(k++, arr[i++]);
else
temp.set(k++, arr[j++]);
}
// Copy the remaining elements of arr[i to index ]
while (i < index)
temp.set(k++, arr[i++]);
// Copy the remaining elements of arr[index to n ]
while (j < n)
temp.set(k++, arr[j++]);
return temp;
}
public static void main(String[] args) {
int[] arr = {2, 3, 8, -1, 7, 10};
int n = arr.length;
ArrayList<Integer> ans = mergeHalves(arr);
for (int i = 0; i < n; i++)
System.out.print(ans.get(i) + " ");
}
}
Python
def mergeHalves(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 arr.copy()
# 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
return temp
if __name__ == "__main__":
arr = [2, 3, 8, -1, 7, 10]
n = len(arr)
ans = mergeHalves(arr)
for i in range(n):
print(ans[i], end=" ")
C#
using System;
using System.Collections.Generic;
public class GFG {
public static List<int> mergeHalves(int[] arr) {
int n = arr.Length;
// starting index of second half
int index = 0;
// Temp Array store sorted resultant array
List<int> temp = new List<int>(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) {
// copy original array into a List to return
List<int> original = new List<int>();
foreach (int x in arr) original.Add(x);
return original;
}
// Merge two sorted arrays in single sorted array
int i1 = 0, j = index, k = 0;
while (i1 < index && j < n) {
if (arr[i1] < arr[j])
temp[k++] = arr[i1++];
else
temp[k++] = arr[j++];
}
// Copy the remaining elements of arr[i to index ]
while (i1 < index)
temp[k++] = arr[i1++];
// Copy the remaining elements of arr[ index to n ]
while (j < n)
temp[k++] = arr[j++];
return temp;
}
public static void Main() {
int[] arr = {2, 3, 8, -1, 7, 10};
int n = arr.Length;
List<int> ans = mergeHalves(arr);
for (int i = 0; i < n; i++)
Console.Write(ans[i] + " ");
}
}
JavaScript
function mergeHalves(arr) {
let n = arr.length;
// starting index of second half
let index = 0;
// Temp Array store sorted resultant array
let temp = new Array(n).fill(0);
// 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 arr.slice(); // return a shallow copy
// 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++];
return temp;
}
// Driver Code
let arr = [2, 3, 8, -1, 7, 10];
let n = arr.length;
let ans = mergeHalves(arr);
for (let i = 0; i < n; i++)
process.stdout.write(ans[i] + " ");
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem