Merge pull request #514 from lq920320/Development

add MergeSortTest for MergeSort.
This commit is contained in:
Varun Upadhyay 2018-09-09 09:41:34 -07:00 committed by GitHub
commit 08dc6adfcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 109 additions and 0 deletions

View File

@ -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 <T> Comparable class
* @return sorted array
*/
@SuppressWarnings("unchecked")
public <T extends Comparable<T>> 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 <T extends Comparable<T>> 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 <T extends Comparable<T>> 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++;
}
}
}

View File

@ -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));
}
}