Implemented using the Priority_queue (implemented by the largest minimum heap) in STL. Note that you need to pass three when passing parameters.
Template declaration: Priority_queue<type, Container, functional>
The implementation here requires the input to be completely correct, so the code is very poorly ported.
#include <iostream>#include<algorithm>#include<queue>using namespaceStd;typedefstruct { intkey; intNext;} Likelist;BOOLComparelikelist (likelist &i1,likelist &I2) { returnI1.key >I2.key;}voidK_merge (int*a,int*b,intNintk) {Priority_queue<likelist,vector<likelist>,decltype (comparelikelist) *>PQ (comparelikelist); for(inti =0; I < K; i++){ if(I > N-1) Pq.push ({Int_max,0 }); ElsePq.push ({a[i], i +k}); } for(inti =0; I < n; ++i) {Likelist temp=Pq.top (); B[i]=Temp.key; Pq.pop (); if(Temp.next>n-1) Pq.push ({Int_max,0 }); ElsePq.push ({A[temp.next], Temp.next +k}); }}intMain () {inta[Ten] = { -,1,8, $,2,9, the,Ten,9,666 }; intb[Ten]; K_merge (A, B,Ten,3); for(auto r:b) cout<< R <<" ";}
Introduction to K-Path merging algorithm 8-4 (e)