-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge k sorted arrays.cpp
52 lines (40 loc) · 1.3 KB
/
merge k sorted arrays.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<bits/stdc++.h>
#define ppi pair<int,pair<int ,int> > // stores { element , { index of array , index of element in array } }
using namespace std;
vector<int> mergeSort(vector<vector<int> > arr)
{
vector<int> res; // stores final result
priority_queue<ppi,vector<ppi>,greater<ppi> > pq; // creating min_heap ( root of heap contains min element )
// insert first element of each array into the pq
for(int i=0;i<arr.size();i++)
{
pq.push({arr[i][0],{i,0}});
}
while(!pq.empty())
{
ppi curr = pq.top();
pq.pop();
int data = curr.first; // element
int i = curr.second.first; // index of array
int j = curr.second.second; // index of element in array
res.push_back(data);
if(j+1<arr[i].size()) // checking if there are more elements present in the array to process
{
pq.push({arr[i][j+1],{i,j+1}});
}
}
return res;
}
int main()
{
vector<vector<int> > arr = {
{2,6,12},
{1,9},
{23, 34, 90, 2000}
};
vector<int> res = mergeSort(arr);
for(int e:res)
{
cout << e << " ";
}
}