Commit 3f017dd2 authored by Fokion Zervoudakis's avatar Fokion Zervoudakis

Simplify implementation. Update test data. Format whitespace.

parent 8e132774
......@@ -19,7 +19,7 @@ class ExternalSort {
lines[i] = line;
if (++i == SIZE) {
var path = outPath + ++j;
write(new MergeSort<String>().sort(lines), path);
write(MergeSort.sort(lines), path);
paths.add(path);
lines = new String[SIZE];
i = 0;
......@@ -28,7 +28,7 @@ class ExternalSort {
}
if (i > 0) {
var path = outPath + ++j;
write(new MergeSort<String>().sort(lines), path);
write(MergeSort.sort(lines), path);
paths.add(path);
}
merge(paths, outPath + ++j);
......@@ -43,18 +43,18 @@ class ExternalSort {
}
/**
* Implements disk-backed, single-pass, naive <em>k-way merging</em>.
* <p>
* Worst-case time complexity: O((k−1)(n−k/2))
* <p>
*
* @param inPaths a list of file paths to sorted input lists
* @param outPath a file path to the final sorted file
* @throws IOException if an I/O error occurs
* @see <a href="https://en.wikipedia.org/wiki/Merge_algorithm#K-way_merging">K-way
* merging</a>
* @see <a href="https://en.wikipedia.org/wiki/External_sorting#Additional_passes">Additional
* passes</a>
Implements disk-backed, single-pass, naive <em>k-way merging</em>.
<p>
Worst-case time complexity: O((k−1)(n−k/2))
<p>
@param inPaths a list of file paths to sorted input lists
@param outPath a file path to the final sorted file
@throws IOException if an I/O error occurs
@see <a href="https://en.wikipedia.org/wiki/Merge_algorithm#K-way_merging">K-way
merging</a>
@see <a href="https://en.wikipedia.org/wiki/External_sorting#Additional_passes">Additional
passes</a>
*/
private void merge(List<String> inPaths, String outPath) throws IOException {
// Initialize the input...
......
......@@ -2,21 +2,21 @@ package sort.external;
import java.util.Arrays;
class MergeSort<T extends Comparable<T>> {
T[] sort(T[] A) {
int n = A.length;
class MergeSort {
static String[] sort(String[] A) {
var n = A.length;
if (n < 2) {
return A;
}
int m = n / 2;
T[] B = Arrays.copyOfRange(A, 0, m);
T[] C = Arrays.copyOfRange(A, m, n);
var m = n / 2;
var B = Arrays.copyOfRange(A, 0, m);
var C = Arrays.copyOfRange(A, m, n);
return merge(sort(B), sort(C));
}
private T[] merge(T[] B, T[] C) {
private static String[] merge(String[] B, String[] C) {
int m = B.length, n = C.length;
T[] A = (T[]) new Comparable[m + n];
var A = new String[m + n];
int i = 0, j = 0, k = 0;
while (j < m && k < n) {
A[i++] = (B[j].compareTo(C[k]) < 0) ? B[j++] : C[k++];
......
package sort.external;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
......@@ -8,52 +7,33 @@ import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.assertEquals;
class MergeSortTest {
private MergeSort<Integer> sort;
@BeforeEach
void beforeEach() {
sort = new MergeSort<>();
}
@Test
void itSortsZeroElements() {
var A = new Integer[0];
assertEquals("[]", Arrays.toString(sort.sort(A)));
var A = new String[0];
assertEquals("[]", Arrays.toString(MergeSort.sort(A)));
}
@Test
void itSortsOneElement() {
var A = new Integer[] { 1 };
assertEquals("[1]", Arrays.toString(sort.sort(A)));
var A = new String[] { "a" };
assertEquals("[a]", Arrays.toString(MergeSort.sort(A)));
}
@Test
void itSortsAnEvenNumberOfElements() {
var A = new Integer[] { 5, 2, 1, 3, 6, 4 };
assertEquals("[1, 2, 3, 4, 5, 6]", Arrays.toString(sort.sort(A)));
var A = new String[] { "e", "b", "a", "c", "f", "d" };
assertEquals("[a, b, c, d, e, f]", Arrays.toString(MergeSort.sort(A)));
}
@Test
void itSortsAnOddNumberOfElements() {
var A = new Integer[] { 5, 2, 1, 3, 4 };
assertEquals("[1, 2, 3, 4, 5]", Arrays.toString(sort.sort(A)));
var A = new String[] { "e", "b", "a", "c", "d" };
assertEquals("[a, b, c, d, e]", Arrays.toString(MergeSort.sort(A)));
}
@Test
void itSortsDuplicateElements() {
var A = new Integer[] { 5, 5, 2, 1, 1, 3, 4, 3, 3 };
assertEquals("[1, 1, 2, 3, 3, 3, 4, 5, 5]", Arrays.toString(sort.sort(A)));
}
@Test
void itSortsNegativeElements() {
var A = new Integer[] { -5, -2, -1, -3, -4 };
assertEquals("[-5, -4, -3, -2, -1]", Arrays.toString(sort.sort(A)));
}
@Test
void itSortsMixedElements() {
var A = new Integer[] { 5, -2, 1, -3, 4 };
assertEquals("[-3, -2, 1, 4, 5]", Arrays.toString(sort.sort(A)));
var A = new String[] { "e", "e", "b", "a", "a", "c", "d", "c", "c" };
assertEquals("[a, a, b, c, c, c, d, e, e]", Arrays.toString(MergeSort.sort(A)));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment