I have n sorted arrays of size n each and I have to merge them.
I am trying to implement this using priority queue as a min heap. Why am I getting wrong output.
Any suggestions . Here is my code:)
[1]
[1]: https://drive.google.com/file/d/18MGuSn3bJsOouDOS1HzW6c1mR5fdFS__/view?usp=sharing
Please help me.
Thanks in advance.
Yes I did.
They were building heap from scratch.
I tried to use priority queue but I am getting wrong answer.
Can you please look into my code for the mistake Sir:)
x.e=a[i][0];
x.i=i;
x.j=1;
You did x.j=1, and later are again incrementing the pointer, basically skipping over an element at start. Here is the other part-
r=t.j;
if(r<n)
{
r++;
q=t.i;
p=a[q][r];
}
Thats the first error.
Second error, again in same part-
if(r<n)
{
r++;
q=t.i;
p=a[q][r];
}
You check if r<n , fine, but then why increment r and then use it? What happens if r=n-1 ? It goes out of bound.
After fixing these two, hopefully your code should work
I made certain changes.
Can you please check if it’s correct now.
Thanks for your time Sire:)
[1]
[1]: https://docs.google.com/document/d/1-fimPrs3zJjy-lpGbQXy5JFL7MKsExNbwAuztNhNaPo/edit?usp=sharing