Though it is possible to iteratively merge arrays, a more efficient way will be a k-way merge, which is done in O(nlogk)
where k
is the number of arrays and n
is the total number of elements, using a priority queue.
Note: It cannot be done better then O(nlogk)
, because if it was possible [let's say with complexity O(g(n))
where g(n)
is as asymptotically weaker then nlogn
] - then we can produce the following sorting algorith:
sort(array A):
split A into n arrays, each of size 1, B1,...,Bn
A <- special_merge(B1,...Bn)
return A
It is easy to see that this algorithm is of complexity O(g(n) + n) = O(g(n))
, and we get a contraditcion, since we got a sort better then O(nlogn)
- which is not possible with compartions based algorithms, since this problem is Omega(nlogn)