Merge two sorted arrays using Priority queue
Given two sorted arrays A[] and B[] of sizes N and M respectively, the task is to merge them in a sorted manner.
Examples:
Input: A[] = { 5, 6, 8 }, B[] = { 4, 7, 8 }
Output: 4 5 6 7 8 8
Input: A[] = {1, 3, 4, 5}, B] = {2, 4, 6, 8}
Output: 1 2 3 4 4 5 6 8
Input: A[] = {5, 8, 9}, B[] = {4, 7, 8}
Output: 4 5 7 8 8 9
Approach: The given problem, merging two sorted arrays using minheap already exists. But here the idea is to use a priority_queue to implement min-heap provided by STL. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void merge( int A[], int B[], int N, int M)
{
int res[N + M];
priority_queue< int , vector< int >, greater< int > > pq;
for ( int i = 0; i < N; i++)
pq.push(A[i]);
for ( int i = 0; i < M; i++)
pq.push(B[i]);
int j = 0;
while (!pq.empty()) {
res[j++] = pq.top();
pq.pop();
}
for ( int i = 0; i < N + M; i++)
cout << res[i] << ' ' ;
}
int main()
{
int A[] = { 5, 6, 8 };
int B[] = { 4, 7, 8 };
int N = sizeof (A) / sizeof (A[0]);
int M = sizeof (B) / sizeof (B[0]);
merge(A, B, N, M);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void merge( int A[], int B[], int N, int M)
{
int []res = new int [N + M];
Queue<Integer> pq = new PriorityQueue<>();
for ( int i = 0 ; i < N; i++)
pq.add(A[i]);
for ( int i = 0 ; i < M; i++)
pq.add(B[i]);
int j = 0 ;
while (!pq.isEmpty())
{
res[j++] = pq.peek();
pq.remove();
}
for ( int i = 0 ; i < N + M; i++)
System.out.print(res[i] + " " );
}
public static void main(String[] args)
{
int A[] = { 5 , 6 , 8 };
int B[] = { 4 , 7 , 8 };
int N = A.length;
int M = B.length;
merge(A, B, N, M);
}
}
|
Python3
from queue import PriorityQueue
def merge(A, B, N, M):
res = [ 0 for i in range (N + M)]
pq = PriorityQueue()
for i in range (N):
pq.put(A[i])
for i in range (M):
pq.put(B[i])
j = 0
while not pq.empty():
res[j] = pq.get()
j + = 1
for i in range (N + M):
print (res[i], end = " " )
return
if __name__ = = '__main__' :
A = [ 5 , 6 , 8 ]
B = [ 4 , 7 , 8 ]
N = len (A)
M = len (B)
merge(A, B, N, M)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static int cmp( int a, int b) { return a - b; }
static void merge( int [] A, int [] B, int N, int M)
{
int [] res = new int [N + M];
List< int > pq = new List< int >();
for ( int i = 0; i < N; i++)
pq.Add(A[i]);
for ( int i = 0; i < M; i++)
pq.Add(B[i]);
int j = 0;
pq.Sort(cmp);
int index = 0;
while (index < pq.Count) {
res[j++] = pq[index];
index++;
}
for ( int i = 0; i < N + M; i++)
Console.Write(res[i] + " " );
}
public static void Main( string [] args)
{
int [] A = { 5, 6, 8 };
int [] B = { 4, 7, 8 };
int N = A.Length;
int M = B.Length;
merge(A, B, N, M);
}
}
|
Javascript
<script>
function merge(A, B, N, M)
{
var res = Array(N+M).fill(0);
var pq = [];
for ( var i = 0; i < N; i++)
pq.push(A[i]);
for ( var i = 0; i < M; i++)
pq.push(B[i]);
var j = 0;
pq.sort((a,b)=>b-a);
while (pq.length!=0) {
res[j++] = pq[pq.length-1];
pq.pop();
pq.sort((a,b)=>b-a);
}
for ( var i = 0; i < N + M; i++)
document.write(res[i] + ' ' );
}
var A = [5, 6, 8];
var B = [4, 7, 8];
var N = A.length;
var M = B.length;
merge(A, B, N, M);
</script>
|
Time Complexity: O((N+M)*log(N+M))
Auxiliary Space: O(N+M)
Last Updated :
18 Oct, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...