algorithm - Why is iterative k-way merge O(nk^2)? -
k-way merge algorithm takes input k sorted arrays, each of size n. outputs single sorted array of elements.
it using "merge" routine central merge sort algorithm merge array 1 array 2, , array 3 merged array, , on until k arrays have merged.
i had thought algorithm o(kn) because algorithm traverses each of k arrays (each of length n) once. why o(nk^2)?
because doesn't traverse each of k arrays once. first array traversed k-1 times, first merge(array-1,array-2), second merge(merge(array-1, array-2), array-3) ... , on.
the result k-1 merges average size of n*(k+1)/2 giving complexity of o(n*(k^2-1)/2) o(nk^2).
the mistake made forgetting merges done serially rather in parallel, arrays not size n.
Comments
Post a Comment