Difference between revisions of "MergeSort"
| Line 1: | Line 1: | ||
=Motivation= | =Motivation= | ||
| − | Merge Sort is a elegant example of a divide-and-conquer algorithm which can be straight- | + | Merge Sort is a elegant example of a divide-and-conquer algorithm which can be parallelized in a straight-forward manner. |
| + | |||
| + | Finally, parallelization of the combine step, while not trivial, is possible (and left as an optional fun exercise for those so inclined). | ||
=Background= | =Background= | ||
Revision as of 03:57, 29 January 2018
Contents
Motivation
Merge Sort is a elegant example of a divide-and-conquer algorithm which can be parallelized in a straight-forward manner.
Finally, parallelization of the combine step, while not trivial, is possible (and left as an optional fun exercise for those so inclined).
Background
In computer science, merge sort refers to a sorting algorithm which splits an array of data into continuously smaller halves until the arrays reach a size of 1, after which the elements are continuously compared and merged into a sorted array. For more information on how this process works, visit this page. If you took CSE 247, this algorithm should look extremely familiar to you.
Visualization
If you've never seen mergesort before, it may be helpful to take a look at this page which gives a visual demonstration of the algorithm in practice.
Utilities
Both the sequential and parallel merge sorts will be passed a Combiner. When you are done with the divide and conquer phases, invoke combiner.combineRange(data, min, mid, maxExclusive) to merge the two sorted sub-problem results.
Code to Implement
| class: | MergeSort.java | |
| methods: | sequentialMergeSort sequentialMergeSortKernel parallelMergeSort parallelMergeSortKernel |
|
| package: | sort.studio.merge | |
| source folder: | student/src/main/java |
method: sequentialMergeSort
(sequential implementation only)
method: sequentialMergeSortKernel
(sequential implementation only)
method: parallelMergeSort
(parallel implementation required)
method: parallelMergeSortKernel
(parallel implementation required)
Optional Fun Parallel Combiner
| class: | ParallelCombiner.java | |
| methods: | parallelCombine | |
| package: | sort.fun.merge | |
| source folder: | student/src/main/java |
You can divide and conquer the combine step in merge sort. The work will remain Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \mathcal{O}(n\lg{}n)} while the critical path length will be improved from Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \mathcal{O}(n)} down to Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \mathcal{O}(\lg^3{}n)} .
Sadly, you cannot simply divide both sides down the middle and merge the lower halves together and the upper halves together (as the data will likely not line up perfectly).
Where to Start
Navigate to the sort.studio.merge package and look at the MergeSort.java class. The sort.core.merg packagecontains some useful building block classes we created for you. Take a look at the MergeableData interface as you will need to use its methods to assist you in this studio.
MergeSort Studio
You will need to implement merge sort sequentially and in parallel, but you will need to do both implementations recursively. The kernel method should call itself using recursion, but the mergesort method should only call the kernel once. We do this in order to manipulate where we can place our async/finish blocks. When completing this studio, think carefully about where to put your async/finish calls.
Sequential Implementation
The only thing you will need to edit is the kernel method. As this is a sequential implementation, there is no need to call an async or finish. Using the description of merge sort provided to you in the background, try to consider what the base case would be. Then, further consider how to call the method recursively. When the array reaches the base case, you will need to complete the “merging” aspect of merge sort. As mentioned above, there is a method in the utils class that should help with this.
Hint: Follow the Wikipedia links and look at the pseudocode for help.
Parallel Implementation
This should look exactly like the sequential implementation, but it should also include async/finish calls. Think carefully about where to place them to maximize concurrency.
Testing Your Solution
Correctness
| class: | MergeSortTestSuite.java | |
| package: | sort.studio.merge | |
| source folder: | testing/src/test/java |
Performance
| class: | SortTiming.java | |
| package: | sort | |
| source folder: | src/main/java |