From 999dc93f2397e954360043fe5be21b3442ca7a1c Mon Sep 17 00:00:00 2001 From: lq Date: Sun, 9 Sep 2018 20:14:32 +0800 Subject: [PATCH] add MergeSortTest for MergeSort. --- src/main/java/com/sorts/MergeSort.java | 75 ++++++++++++++++++++++ src/test/java/com/sorts/MergeSortTest.java | 34 ++++++++++ 2 files changed, 109 insertions(+) create mode 100644 src/main/java/com/sorts/MergeSort.java create mode 100644 src/test/java/com/sorts/MergeSortTest.java diff --git a/src/main/java/com/sorts/MergeSort.java b/src/main/java/com/sorts/MergeSort.java new file mode 100644 index 00000000..3582a08b --- /dev/null +++ b/src/main/java/com/sorts/MergeSort.java @@ -0,0 +1,75 @@ +package src.main.java.com.sorts; + +public class MergeSort { + + /** + * This method implements the Generic Merge Sort + * + * @param unsorted the array which should be sorted + * @param Comparable class + * @return sorted array + */ + @SuppressWarnings("unchecked") + public > T[] sort(T[] unsorted) { + T[] tmp = (T[]) new Comparable[unsorted.length]; + doSort(unsorted, tmp, 0, unsorted.length - 1); + return unsorted; + } + + /** + * @param arr The array to be sorted + * @param temp The copy of the actual array + * @param left The first index of the array + * @param right The last index of the array + * Recursively sorts the array in increasing order + **/ + private static > void doSort(T[] arr, T[] temp, int left, int right) { + if (left < right) { + int mid = left + (right - left) / 2; + doSort(arr, temp, left, mid); + doSort(arr, temp, mid + 1, right); + merge(arr, temp, left, mid, right); + } + } + + /** + * This method implements the merge step of the merge sort + * + * @param arr The array to be sorted + * @param temp The copy of the actual array + * @param left The first index of the array + * @param mid The middle index of the array + * @param right The last index of the array + * merges two parts of an array in increasing order + **/ + private static > void merge(T[] arr, T[] temp, int left, int mid, int right) { + System.arraycopy(arr, left, temp, left, right - left + 1); + + int i = left; + int j = mid + 1; + int k = left; + + while (i <= mid && j <= right) { + if (temp[i].compareTo(temp[j]) <= 0) { + arr[k] = temp[i]; + i++; + } else { + arr[k] = temp[j]; + j++; + } + k++; + } + + while (i <= mid) { + arr[k] = temp[i]; + i++; + k++; + } + + while (j <= right) { + arr[k] = temp[j]; + j++; + k++; + } + } +} diff --git a/src/test/java/com/sorts/MergeSortTest.java b/src/test/java/com/sorts/MergeSortTest.java new file mode 100644 index 00000000..25851523 --- /dev/null +++ b/src/test/java/com/sorts/MergeSortTest.java @@ -0,0 +1,34 @@ +package src.test.java.com.sorts; + + +import org.junit.Assert; +import org.junit.Test; +import src.main.java.com.sorts.MergeSort; + +public class MergeSortTest { + + @Test + public void mergeSortTest() { + MergeSort mergeSort = new MergeSort(); + + Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7}; + Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + Assert.assertArrayEquals(sortedInt, mergeSort.sort(unsortedInt)); + + unsortedInt = new Integer[]{5, 4, 3, 2, 1, 0}; + sortedInt = new Integer[]{0, 1, 2, 3, 4, 5}; + Assert.assertArrayEquals(sortedInt, mergeSort.sort(unsortedInt)); + + unsortedInt = new Integer[]{-1, -2, -3, -4, -5}; + sortedInt = new Integer[]{-5, -4, -3, -2, -1}; + Assert.assertArrayEquals(sortedInt, mergeSort.sort(unsortedInt)); + + unsortedInt = new Integer[]{-1, -5, -10, -990, 990, 1010}; + sortedInt = new Integer[]{-990, -10, -5, -1, 990, 1010}; + Assert.assertArrayEquals(sortedInt, mergeSort.sort(unsortedInt)); + + Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'}; + Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}; + Assert.assertArrayEquals(sortedChar, mergeSort.sort(unsortedChar)); + } +}