MergeSort
Contents
Motivation
Sorting is a problem that is well solved by divide and conquer algorithms. Merge sort is a elegant example 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 the wikipedia page on merge sort.
Visualization
If you are unclear on how merge sort works, take a look at the visualgo explanation and visualization of merge sort.
The Core Questions
- What are the tasks?
- What is the data?
- Is the data mutable?
- If so, how is it shared?
Mistakes To Avoid
Code to Use
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
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 each public mergesort method should only call its kernel once to do the work.
Sorter
| class: | Sorter.java | |
| methods: | sort | |
| package: | sort.core | |
| source folder: | student/src/main/java |
Here you will provide the default implementation of the sort method.
default void sort(T[] data, Comparator<T> comparator)
You should simply invoke the sortRange method with the correct arguments.
SequentialCombiner
| class: | SequentialCombiner.java | |
| methods: | combineRangeIntoBuffer | |
| package: | sort.merge.studio | |
| source folder: | student/src/main/java |
Merge sort's combine step leverages the fact that it can rely on the two sub-ranges are provided in sorted order. Use this fact to fill the provided temporary buffer with the sorted contents of the entire range [min, maxExclusive).
Performance note: in an effort to make this part of the assignment more appropriately approachable, a temporary buffer will be created, combined into, and copied back from. Merge sort can be built such that depths of the recursion alternate which buffer is read from and written to. The added complexity this performance improvement would incur is not deemed valuable from a pedagogical stand point, but it is worth noting.
The public method combineRange(data, comparator, min, mid, maxExclusive) is provided:
@Override
public void combineRange(T[] data, Comparator<T> comparator, int min, int mid, int maxExclusive) {
int rangeLength = maxExclusive - min;
T[] buffer = ArrayUtils.createArray(componentType, rangeLength);
combineRangeIntoBuffer(buffer, data, comparator, min, mid, maxExclusive);
System.arraycopy(buffer, 0, data, min, maxExclusive - min);
}
It creates a temporary buffer and passes it to combineRangeIntoBuffer(buffer, data, comparator, min, mid, maxExclusive) which you will implement to fill the temporary buffer with the combined sorted contents. The contents of the temporary buffer is then copied back to the data array.
SequentialMergeSorter
| class: | SequentialMergeSorter.java | |
| methods: | sortRange | |
| package: | sort.merge.studio | |
| source folder: | student/src/main/java |
Implement the classic divide and conquer sequential algorithm here.
private final Combiner<T> combiner; field to perform the combine step. |
ParallelMergeSorter
| class: | ParallelMergeSorter.java | |
| methods: | sortRange | |
| package: | sort.merge.studio | |
| source folder: | student/src/main/java |
Adapt your sequential algorithm to take advantage of what can be parallelized.
What can be in parallel?
What is dependent on the parallel tasks completing?
private final IntPredicate isParallelDesired; field to test whether or not parallelism is warranted for the current range length. |
private final Sorter<T> sequentialSorter; field when the isParallelDesired predicate indicates that parallelism is not longer warranted. |
(Optional) Challenge Parallel Combiner
You can divide and conquer the combine step in merge sort. The work should 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 can be cleanly 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)} .
For details on how to complete this challenge, check out: MergeSort_Parallel_Combiner
Testing Your Solution
Correctness
| class: | _MergeSorterTestSuite.java | |
| package: | sort.merge.studio | |
| source folder: | testing/src/test/java |
Performance
| class: | SortTiming.java | |
| package: | sort | |
| source folder: | src/main/java |
Note: do not be concerned if your implementations run slower than Java's highly optimized Dual Pivot Quicksort.
Pledge, Acknowledgments, Citations
| file: | studio-merge-sort-pledge-acknowledgments-citations.txt |
More info about the Honor Pledge