Problem

Segment 9.8 introduced you to an iterative merge sort. This project continues that discuss...

Segment 9.8 introduced you to an iterative merge sort. This project continues that discussion by providing more details about the merge steps.

a. If n is a power of 2, as it is in Figure 2, you would merge pairs of individual entries, starting at the beginning of the array. Then you would return to the beginning of the array and merge pairs of twoentry subarrays. Finally, you would merge one pair of four-entry subarrays. Notice that the subarrays in each pair of subarrays contain the same number of entries.

In general, n might not be a power of 2. After merging a certain number of pairs of subarrays, you might have too few entries left to make up a complete pair of subarrays. In Figure 3a, after merging pairs of single entries, one entry is left over. You then merge one pair of two-entry subarrays, and merge the leftover two-entry subarray with the leftover single entry. Parts b and c of Figure 3 show two other possibilities.

Implement an iterative merge sort. Use the algorithm merge that was given in Segment 9.3. A private method that uses merge to merge the pairs of subarrays is useful. After the method completes its task, you can handle the leftovers that we just described.


b. Merging two subarrays requires an additional temporary array. Although you need to use this extra space, you can save much of the time that our earlier merge algorithm spends in copying entries from the temporary array back to the original array. If a is the original array and t is the temporary array, you first merge subarrays of a into the array t. Instead of copying t back to a and continuing the merge, you determine subarrays of t and merge them into a. If you can do this an even number of times, no additional copying is necessary. Make these changes to the iterative merge sort that you wrote in Part a.

FIGURE 2 The effect of the recursive calls and the merges during a merge sort

FIGURE 3 Special cases in an iterative merge sort after merging one-entry subarrays

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 9
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT