# While HeapSort is more amenable than MergeSort to an in-place implementation, MergeSort has its o…

##### Home/Education/Expert Q&A/While HeapSort is more amenable than MergeSort to an in-place implementation, MergeSort has its o…

**Question:**

While HeapSort is more amenable than MergeSort to an in-place implementation, MergeSort has its own advantages. For this problem, suppose you want to perform MergeSort on a really huge array A. The array is so big that it doesn’t fit in your computer’s memory and has to be stored in the cloud. More specifically, assume that our computer has enough memory to hold 3b elements, for some constant b, but A has size n much greater than b. We can call **read(X, i, B)** to read a chunk of b elements from an array X (in the cloud) starting at index i into a local array B. Similarly, we can call **write(C, X, i)** to write a chunk of b elements stored in local array C to a cloud array X starting at index i.

Here’s a proposed (incomplete!) implementation of the merge operation that merges cloud arrays X and Y into cloud array Z. The code uses local arrays A, B, and C, each of size b, to cache X, Y, and Z. For simplicity, we assume that the input arrays X and Y have sizes a multiple of b, and that reading past the end of either X or Y returns values ∞ as in the studio. “mod” is the integer modulo operator (% in Java).

**Pseudo code****:**

1. Supply blocks of pseudo code to replace the lines “FOO” and “BAR” to complete the implementation of the merge operation. Consider which parts of the regular merge algorithm are missing, and when/how to fetch more data from the cloud?

2. Exactly how many times must the merge function call each of read and write to merge two arrays of size n/2 into an array of size n, assuming that n/2 is divisible by b?

**Click the button below to view answer!**

If you happen to run into some problem while following the steps, please make sure to let us know in the comment section below, and we’ll do our best to solve it. Apart from that, you can contact us on Facebook and Twitter, however we can’t guarantee a rapid reaction time over those platform

## 0 Comments