What is the time complexity of a k-way merge?

2019-08-28 22:17发布

I'm trying to understand the time complexity of a k-way merge using a heap, and although there is a plethora of literature available on it, I can't find one that breaks down the analysis such that I can understand.

This Wikipedia article claims that "In an O(k) preprocessing step the heap is created using the standard heapify procedure". However, heap insertion is O(log(n)) and find-min is O(1). We start by inserting the first elements of each array into the heap. This takes ∑log(i) time, i = 0 to k - 1 or O(klog(k)) time, that refutes the Wikipedia complexity analysis. (actually O(log(k!)))

We then remove the min element, and insert the next element from the array where the min element originally came from. This takes O(1) + O(log(k)) time, which we repeat n - 1 times. Overall time:

O(klog(k)) + O(n - 1) + O((n - 1)log(k)) ≅ O(klog(k)) + O(n) + O(nlog(k))

Wikipedia claims: "the total running time is O(n log k)". How is that?

0条回答
登录 后发表回答