
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Merge k sorted arrays of different sizes in C++
Suppose we have different k sorted arrays. We have to merge these arrays and display the sorted result.
So, if the input is like k = 3 and arrays are {2, 4},{3, 5, 7},{1, 10, 11, 12} , then the output will be 1 2 3 4 5 7 10 11 12
To solve this, we will follow these steps −
- define one type of pair with first element is an integer and second element is another pair of integers, name it as ppi.
- Define an array op
- define one priority queue q
- for initialize i := 0, when i < size of arr, update (increase i by 1), do −
- insert (arr[i, 0], {i, 0} )into q
- while q is not empty, do −
- current_element := top element of q
- delete element from q
- i := second element from current_element
- j := third element from current_element
- insert first element of current_element at the end of op
- if j + 1 < size of arr[i], then &minus
- insert (arr[i, j+1], {i, j+1} )into q
- return op
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; #define ppi pair<int,pair<int,int>> vector<int> merge(vector<vector<int> > arr){ vector<int> op; priority_queue<ppi, vector<ppi>, greater<ppi> > queue; for (int i = 0; i < arr.size(); i++) queue.push({ arr[i][0], { i, 0 } }); while (queue.empty() == false) { ppi current_element = queue.top(); queue.pop(); int i = current_element.second.first; int j = current_element.second.second; op.push_back(current_element.first); if (j + 1 < arr[i].size()) queue.push({ arr[i][j + 1], { i, j + 1 } }); } return op; } int main(){ vector<vector<int> > arr{ { 2,4}, { 3,5,7 }, { 1, 10, 11, 12 } }; vector<int> output = merge(arr); for(int i = 0; i<output.size(); i++) cout << output[i] << " "; }
Input
{{ 2,4}, { 3,5,7 }, { 1, 10, 11, 12 }}
Output
1 2 3 4 5 7 10 11 12
- Related Articles
- Merge k sorted arrays in Java
- Merge two sorted arrays in Java
- Merge two sorted arrays in C#
- Merge k Sorted Lists in Python
- Merge two sorted arrays using C++.
- Merge K sorted linked lists in Java
- Merge two sorted arrays in Python using heapq?
- Program to merge K-sorted lists in Python
- Merge two sorted arrays to form a resultant sorted array in JavaScript
- How to create three-dimensional arrays of different sizes in R?
- K-th Element of Two Sorted Arrays in C++
- Merge two sorted arrays into a list using C#
- C# program to merge two sorted arrays into one
- Minimize (max(A[i], B[j], C[k]) – min(A[i], B[j], C[k])) of three different sorted arrays in Python
- Minimize (max(A[i], B[j], C[k]) – min(A[i], B[j], C[k])) of three different sorted arrays in C++

Advertisements