Give k sorted inverted lists, I want an efficient algorithm to get the union of these k lists? Each inverted list is a read-only array in memory, each list contains integer in sorted order. the result will be saved in a predefined array which is large enough. Is there any algorithm better than k-way merge?
K-Way merge is optimal. It has O(log(k)*n)
ops [where n
is the number of elements in all lists combined].
It is easy to see it cannot be done better - as @jpalecek mentioned, otherwise you could sort any array better then O(nlogn)
by splitting it into chunks [inverted indexes] of size 1.