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

Skip to content

Commit 08dc6ad

Browse files
authored
Merge pull request TheAlgorithms#514 from lq920320/Development
add MergeSortTest for MergeSort.
2 parents 4e7843b + 999dc93 commit 08dc6ad

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package src.main.java.com.sorts;
2+
3+
public class MergeSort {
4+
5+
/**
6+
* This method implements the Generic Merge Sort
7+
*
8+
* @param unsorted the array which should be sorted
9+
* @param <T> Comparable class
10+
* @return sorted array
11+
*/
12+
@SuppressWarnings("unchecked")
13+
public <T extends Comparable<T>> T[] sort(T[] unsorted) {
14+
T[] tmp = (T[]) new Comparable[unsorted.length];
15+
doSort(unsorted, tmp, 0, unsorted.length - 1);
16+
return unsorted;
17+
}
18+
19+
/**
20+
* @param arr The array to be sorted
21+
* @param temp The copy of the actual array
22+
* @param left The first index of the array
23+
* @param right The last index of the array
24+
* Recursively sorts the array in increasing order
25+
**/
26+
private static <T extends Comparable<T>> void doSort(T[] arr, T[] temp, int left, int right) {
27+
if (left < right) {
28+
int mid = left + (right - left) / 2;
29+
doSort(arr, temp, left, mid);
30+
doSort(arr, temp, mid + 1, right);
31+
merge(arr, temp, left, mid, right);
32+
}
33+
}
34+
35+
/**
36+
* This method implements the merge step of the merge sort
37+
*
38+
* @param arr The array to be sorted
39+
* @param temp The copy of the actual array
40+
* @param left The first index of the array
41+
* @param mid The middle index of the array
42+
* @param right The last index of the array
43+
* merges two parts of an array in increasing order
44+
**/
45+
private static <T extends Comparable<T>> void merge(T[] arr, T[] temp, int left, int mid, int right) {
46+
System.arraycopy(arr, left, temp, left, right - left + 1);
47+
48+
int i = left;
49+
int j = mid + 1;
50+
int k = left;
51+
52+
while (i <= mid && j <= right) {
53+
if (temp[i].compareTo(temp[j]) <= 0) {
54+
arr[k] = temp[i];
55+
i++;
56+
} else {
57+
arr[k] = temp[j];
58+
j++;
59+
}
60+
k++;
61+
}
62+
63+
while (i <= mid) {
64+
arr[k] = temp[i];
65+
i++;
66+
k++;
67+
}
68+
69+
while (j <= right) {
70+
arr[k] = temp[j];
71+
j++;
72+
k++;
73+
}
74+
}
75+
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package src.test.java.com.sorts;
2+
3+
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
import src.main.java.com.sorts.MergeSort;
7+
8+
public class MergeSortTest {
9+
10+
@Test
11+
public void mergeSortTest() {
12+
MergeSort mergeSort = new MergeSort();
13+
14+
Integer[] unsortedInt = new Integer[]{0, 5, 9, 2, 1, 3, 4, 8, 6, 7};
15+
Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
16+
Assert.assertArrayEquals(sortedInt, mergeSort.sort(unsortedInt));
17+
18+
unsortedInt = new Integer[]{5, 4, 3, 2, 1, 0};
19+
sortedInt = new Integer[]{0, 1, 2, 3, 4, 5};
20+
Assert.assertArrayEquals(sortedInt, mergeSort.sort(unsortedInt));
21+
22+
unsortedInt = new Integer[]{-1, -2, -3, -4, -5};
23+
sortedInt = new Integer[]{-5, -4, -3, -2, -1};
24+
Assert.assertArrayEquals(sortedInt, mergeSort.sort(unsortedInt));
25+
26+
unsortedInt = new Integer[]{-1, -5, -10, -990, 990, 1010};
27+
sortedInt = new Integer[]{-990, -10, -5, -1, 990, 1010};
28+
Assert.assertArrayEquals(sortedInt, mergeSort.sort(unsortedInt));
29+
30+
Character[] unsortedChar = new Character[]{'f', 'h', 'c', 'a', 'b', 'd', 'g', 'e'};
31+
Character[] sortedChar = new Character[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
32+
Assert.assertArrayEquals(sortedChar, mergeSort.sort(unsortedChar));
33+
}
34+
}

0 commit comments

Comments
 (0)