Thanks to visit codestin.com
Credit goes to github.com

Skip to content

add MergeSortTest for MergeSort. #514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions src/main/java/com/sorts/MergeSort.java
Original file line number Diff line number Diff line change
@@ -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++;
}
}
}
34 changes: 34 additions & 0 deletions src/test/java/com/sorts/MergeSortTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}