Solution Set 3
Solutions to problem set 3
We'll cover the following...
Solution 1
a-
We can implement merge sort by dividing the initial input array into 3 subproblems instead of 2. The only caution we need to exercise is to carefully pass the boundaries for the three parts in the subsequent recursive portions. Combining the three arrays is equivalent of solving the problem of merging n number of sorted arrays, using a priority queue. One way the algorithm can be implemented is shown below.
b-
How many recursion levels will the be generated?
Say we are provided with an array of size n. The question we need to ask ourselves is how many times do we need to successively divide n by 3 to get to 1? If there are 9 elements, we'll divide once to get 3 and then one more time to get 1. This can be expressed as a logarithmic equation as follow:
The number of levels of the recursion tree will be 1 more than log3n so the correct answer would be log3n + 1.
c-
The recurrence equation will be given as:
We determined in the previous question the number of recursion levels for the 3-way merge sort will be log3n+1. Next, we need to determine the cost to merge the three subproblems. Unlike in traditional merge sort, the 3-way merge sort uses a priority queue to create a min-heap before attempting a merge of the three subproblems. Insertion into a priority queue takes lgn time and since we insert all the n elements into the queue, the total time to create the heap comes out to be nlgn. Finally, the cost to divide the problem into three subproblems is constant and can be represented by d. Therefore, the time complexity will be:
Since lgn > log3n, we can simplify to:
...