Hey everyone, I’m working on a small utility to merge several sorted arrays, and I’m using a min-heap so I don’t have to flatten everything first. It mostly works, but on duplicate values I sometimes lose items, which is worse than being a little slower.
function mergeSorted(lists) {
const heap = [];
const out = [];
function push(node) {
heap.push(node);
heap.sort((a, b) => a.value - b.value);
}
for (let i = 0; i < lists.length; i++) {
if (lists[i].length) {
push({ value: lists[i][0], list: i, index: 0 });
}
}
while (heap.length) {
const cur = heap.shift();
out.push(cur.value);
const nextIndex = cur.index + 1;
if (lists[cur.list][nextIndex]) {
push({ value: lists[cur.list][nextIndex], list: cur.list, index: nextIndex });
}
}
return out;
}
console.log(mergeSorted([[1, 4, 4], [1, 3], [2, 4]]));
What am I overlooking here that makes some valid next values disappear when merging sorted arrays?
Hari ![]()