Published on

The Algorithm Design Manual (3rd) Exercise Solution 'E4-30'

Table of Contents

Introduction

This series is my solutions for algorithm exercises in'The Algorithm Design Manual' 3rd edition.
It's my solutions, not model answers, so if you find some mistakes or more sophisticated solutions, please post in comment area below.

Repository

Here

Exercise 4-30

Question

Consider again the problem of merging kk sorted length-nn arrays into a single sorted length-knkn array. Consider the algorithm that first divides the kk arrays into k/2k/2 pairs of arrays, and uses the merge subroutine to combine each pair, resulting in k/2k/2 sorted length-2n2n arrays. The algorithm repeats this step until there is only one length-kn sorted array. What is the running time as a function of nn and kk?

Solution

Let hh be the height og the repetition operation of merging of two arrays of equal length.
By using the same analysis of Mergesort, the constraint of hh is:

2k1<k<2k    h=lgk\begin{equation} 2^{k-1} < k < 2^k \implies h=\lceil\lg{k}\rceil \end{equation}

In each height of repetition, merging takes O(kn)O(kn) time.

So, Running-time is O(knlogk)O(kn\log{k}).