Segment 16.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 16-3, you...


Segment 16.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 16-3, 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 two-entry 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 16-12a (on the next page), 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 16-12 show two other possibilities.


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


Implement an iterative merge sort. Use the algorithm merge that was given in Segment 16.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.

Dec 05, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here