Write a SortedMerge() function that takes two lists, each of which is unsorted, and merges the two together into one new list which is in sorted (increasing) order. SortedMerge() should return the new list.
Examples :
Input : a[] = {10, 5, 15}
b[] = {20, 3, 2}
Output : Merge List :
{2, 3, 5, 10, 15, 20}
Input : a[] = {1, 10, 5, 15}
b[] = {20, 0, 2}
Output : Merge List :
{0, 1, 2, 5, 10, 15, 20}
There are many cases to deal with: either ‘a’ or ‘b’ may be empty, during processing either ‘a’ or ‘b’ may run out first, and finally, there’s the problem of starting the result list empty and building it up while going through ‘a’ and ‘b’.
Method 1 (first Concatenate then Sort): In this case, we first append the two unsorted lists. Then we simply sort the concatenated list.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void sortedMerge( int a[], int b[], int res[],
int n, int m)
{
int i = 0, j = 0, k = 0;
while (i < n) {
res[k] = a[i];
i += 1;
k += 1;
}
while (j < m) {
res[k] = b[j];
j += 1;
k += 1;
}
sort(res, res + n + m);
}
int main()
{
int a[] = { 10, 5, 15 };
int b[] = { 20, 3, 2, 12 };
int n = sizeof (a) / sizeof (a[0]);
int m = sizeof (b) / sizeof (b[0]);
int res[n + m];
sortedMerge(a, b, res, n, m);
cout << "Sorted merged list :" ;
for ( int i = 0; i < n + m; i++)
cout << " " << res[i];
cout << "n" ;
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void sortedMerge( int a[], int b[],
int res[], int n,
int m)
{
int i = 0 , j = 0 , k = 0 ;
while (i < n) {
res[k] = a[i];
i++;
k++;
}
while (j < m) {
res[k] = b[j];
j++;
k++;
}
Arrays.sort(res);
}
public static void main(String[] args)
{
int a[] = { 10 , 5 , 15 };
int b[] = { 20 , 3 , 2 , 12 };
int n = a.length;
int m = b.length;
int res[]= new int [n + m];
sortedMerge(a, b, res, n, m);
System.out.print( "Sorted merged list :" );
for ( int i = 0 ; i < n + m; i++)
System.out.print( " " + res[i]);
}
}
|
Python3
def sortedMerge(a, b, res, n, m):
i, j, k = 0 , 0 , 0
while (i < n):
res[k] = a[i]
i + = 1
k + = 1
while (j < m):
res[k] = b[j]
j + = 1
k + = 1
res.sort()
a = [ 10 , 5 , 15 ]
b = [ 20 , 3 , 2 , 12 ]
n = len (a)
m = len (b)
res = [ 0 for i in range (n + m)]
sortedMerge(a, b, res, n, m)
print ( "Sorted merged list :" )
for i in range (n + m):
print (res[i],end = " " )
|
C#
using System;
class GFG {
public static void sortedMerge( int []a, int []b,
int []res, int n,
int m)
{
int i = 0, j = 0, k = 0;
while (i < n) {
res[k] = a[i];
i++;
k++;
}
while (j < m) {
res[k] = b[j];
j++;
k++;
}
Array.Sort(res);
}
public static void Main()
{
int []a = {10, 5, 15};
int []b = {20, 3, 2, 12};
int n = a.Length;
int m = b.Length;
int []res= new int [n + m];
sortedMerge(a, b, res, n, m);
Console.Write( "Sorted merged list :" );
for ( int i = 0; i < n + m; i++)
Console.Write( " " + res[i]);
}
}
|
PHP
<?php
function sortedMerge( $a , $b , $n , $m )
{
$res = array ();
$i = 0; $j = 0; $k = 0;
while ( $i < $n )
{
$res [ $k ] = $a [ $i ];
$i += 1;
$k += 1;
}
while ( $j < $m )
{
$res [ $k ] = $b [ $j ];
$j += 1;
$k += 1;
}
sort( $res );
echo "Sorted merged list :" ;
for ( $i = 0; $i < count ( $res ); $i ++)
echo $res [ $i ] . " " ;
}
$a = array ( 10, 5, 15 );
$b = array ( 20, 3, 2, 12 );
$n = count ( $a );
$m = count ( $b );
sortedMerge( $a , $b , $n , $m );
?>
|
Javascript
<script>
function sortedMerge(a, b, res,
n, m)
{
a.sort((a,b) => a-b);
b.sort((a,b) => a-b);
let i = 0, j = 0, k = 0;
while (i < n && j < m) {
if (a[i] <= b[j]) {
res[k] = a[i];
i += 1;
k += 1;
} else {
res[k] = b[j];
j += 1;
k += 1;
}
}
while (i < n) {
res[k] = a[i];
i += 1;
k += 1;
}
while (j < m) {
res[k] = b[j];
j += 1;
k += 1;
}
}
let a = [ 10, 5, 15 ];
let b = [ 20, 3, 2, 12 ];
let n = a.length;
let m = b.length;
let res = new Array(n + m);
sortedMerge(a, b, res, n, m);
document.write( "Sorted merge list :" );
for (let i = 0; i < n + m; i++)
document.write( " " + res[i]);
</script>
|
Output
Sorted merged list : 2 3 5 10 12 15 20n
Time Complexity: O ( (n + m) (log(n + m)) )
Auxiliary Space: O ( (n + m) )
Method 2 (First Sort then Merge): We first sort both the given arrays separately. Then we simply merge two sorted arrays.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void sortedMerge( int a[], int b[], int res[],
int n, int m)
{
sort(a, a + n);
sort(b, b + m);
int i = 0, j = 0, k = 0;
while (i < n && j < m) {
if (a[i] <= b[j]) {
res[k] = a[i];
i += 1;
k += 1;
} else {
res[k] = b[j];
j += 1;
k += 1;
}
}
while (i < n) {
res[k] = a[i];
i += 1;
k += 1;
}
while (j < m) {
res[k] = b[j];
j += 1;
k += 1;
}
}
int main()
{
int a[] = { 10, 5, 15 };
int b[] = { 20, 3, 2, 12 };
int n = sizeof (a) / sizeof (a[0]);
int m = sizeof (b) / sizeof (b[0]);
int res[n + m];
sortedMerge(a, b, res, n, m);
cout << "Sorted merge list :" ;
for ( int i = 0; i < n + m; i++)
cout << " " << res[i];
cout << "n" ;
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void sortedMerge( int a[], int b[],
int res[], int n,
int m)
{
Arrays.sort(a);
Arrays.sort(b);
int i = 0 , j = 0 , k = 0 ;
while (i < n && j < m) {
if (a[i] <= b[j]) {
res[k] = a[i];
i += 1 ;
k += 1 ;
} else {
res[k] = b[j];
j += 1 ;
k += 1 ;
}
}
while (i < n) {
res[k] = a[i];
i += 1 ;
k += 1 ;
}
while (j < m) {
res[k] = b[j];
j += 1 ;
k += 1 ;
}
}
public static void main(String[] args)
{
int a[] = { 10 , 5 , 15 };
int b[] = { 20 , 3 , 2 , 12 };
int n = a.length;
int m = b.length;
int res[] = new int [n + m];
sortedMerge(a, b, res, n, m);
System.out.print( "Sorted merged list :" );
for ( int i = 0 ; i < n + m; i++)
System.out.print( " " + res[i]);
}
}
|
Python3
def sortedMerge(a, b, res, n, m):
a.sort()
b.sort()
i, j, k = 0 , 0 , 0
while (i < n and j < m):
if (a[i] < = b[j]):
res[k] = a[i]
i + = 1
k + = 1
else :
res[k] = b[j]
j + = 1
k + = 1
while (i < n):
res[k] = a[i]
i + = 1
k + = 1
while (j < m):
res[k] = b[j]
j + = 1
k + = 1
a = [ 10 , 5 , 15 ]
b = [ 20 , 3 , 2 , 12 ]
n = len (a)
m = len (b)
res = [ 0 for i in range (n + m)]
sortedMerge(a, b, res, n, m)
print ( "Sorted merged list :" )
for i in range (n + m):
print (res[i],end = " " )
|
C#
using System;
class GFG {
static void sortedMerge( int []a, int []b,
int []res, int n, int m)
{
Array.Sort(a);
Array.Sort(b);
int i = 0, j = 0, k = 0;
while (i < n && j < m)
{
if (a[i] <= b[j])
{
res[k] = a[i];
i += 1;
k += 1;
}
else
{
res[k] = b[j];
j += 1;
k += 1;
}
}
while (i < n)
{
res[k] = a[i];
i += 1;
k += 1;
}
while (j < m)
{
res[k] = b[j];
j += 1;
k += 1;
}
}
public static void Main()
{
int []a = { 10, 5, 15 };
int []b = { 20, 3, 2, 12 };
int n = a.Length;
int m = b.Length;
int []res = new int [n + m];
sortedMerge(a, b, res, n, m);
Console.Write( "Sorted merged list :" );
for ( int i = 0; i < n + m; i++)
Console.Write( " " + res[i]);
}
}
|
Javascript
<script>
function sortedMerge(a, b, res, n, m)
{
a.sort((a, b) => a - b);
b.sort((a, b) => a - b);
let i = 0, j = 0, k = 0;
while (i < n && j < m)
{
if (a[i] <= b[j])
{
res[k] = a[i];
i += 1;
k += 1;
}
else
{
res[k] = b[j];
j += 1;
k += 1;
}
}
while (i < n)
{
res[k] = a[i];
i += 1;
k += 1;
}
while (j < m)
{
res[k] = b[j];
j += 1;
k += 1;
}
}
let a = [ 10, 5, 15 ];
let b = [ 20, 3, 2, 12 ];
let n = a.length;
let m = b.length;
let res = new Array(n + m);
sortedMerge(a, b, res, n, m);
document.write( "Sorted merge list :" );
for (let i = 0; i < n + m; i++)
document.write( " " + res[i]);
</script>
|
Output
Sorted merge list : 2 3 5 10 12 15 20n
Time Complexity: O (nlogn + mlogm + (n + m))
Space Complexity: O ( (n + m) )
It is obvious from above time complexities that method 2 is better than method 1.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
19 Sep, 2023
Like Article
Save Article