Skip to content

Commit b6314a4

Browse files
add merge sort
1 parent 7c04229 commit b6314a4

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.examplehub.sorts;
2+
3+
public class MergeSort implements Sort {
4+
5+
@Override
6+
public void sort(int[] numbers) {
7+
mergeSort(numbers, 0, numbers.length - 1);
8+
}
9+
10+
@Override
11+
public <T extends Comparable<T>> void sort(T[] array) {
12+
mergeSort(array, 0, array.length - 1);
13+
}
14+
15+
/**
16+
* Merge sort algorithm implements.
17+
*
18+
* @param numbers the numbers to be sorted.
19+
* @param left the left index of sub array.
20+
* @param right the right index of sub array.
21+
*/
22+
public static void mergeSort(int[] numbers, int left, int right) {
23+
if (left < right) {
24+
int middle = (left + right) >> 1;
25+
mergeSort(numbers, left, middle);
26+
mergeSort(numbers, middle + 1, right);
27+
merge(numbers, left, middle, right);
28+
}
29+
}
30+
31+
/**
32+
* Generic MergeSort algorithm implements.
33+
*
34+
* @param array the array to be sorted.
35+
* @param left the left index of sub array.
36+
* @param right the right index of sub array.
37+
* @param <T> the class of the objects in the array.
38+
*/
39+
public static <T extends Comparable<T>> void mergeSort(T[] array, int left, int right) {
40+
if (left < right) {
41+
int middle = (left + right) >> 1;
42+
mergeSort(array, left, middle);
43+
mergeSort(array, middle + 1, right);
44+
merge(array, left, middle, right);
45+
}
46+
}
47+
48+
/**
49+
* @param numbers the numbers to be merged.
50+
* @param left the left index of first sub array.
51+
* @param middle the right index of second sub array.
52+
* @param right the right index of second sub array.
53+
*/
54+
public static void merge(int[] numbers, int left, int middle, int right) {
55+
int i = left;
56+
int j = middle + 1;
57+
int k = left;
58+
while (i <= middle && j <= right) {
59+
if (numbers[i] <= numbers[j]) {
60+
numbers[k++] = numbers[i++];
61+
} else {
62+
numbers[k++] = numbers[j++];
63+
}
64+
}
65+
while (i <= middle) {
66+
numbers[k++] = numbers[i++];
67+
}
68+
69+
while (j <= right) {
70+
numbers[k++] = numbers[j++];
71+
}
72+
}
73+
74+
/**
75+
* Generic merge two sub sorted numbers.
76+
*
77+
* @param array the array to be sorted.
78+
* @param left the left index of first sub array.
79+
* @param middle the right index of second sub array.
80+
* @param right the right index of second sub array.
81+
* @param <T> the class of the objects in the array.
82+
*/
83+
public static <T extends Comparable<T>> void merge(T[] array, int left, int middle, int right) {
84+
int i = left;
85+
int j = middle + 1;
86+
int k = left;
87+
while (i <= middle && j <= right) {
88+
if (array[i].compareTo(array[j]) <= 0) {
89+
array[k++] = array[i++];
90+
} else {
91+
array[k++] = array[j++];
92+
}
93+
}
94+
while (i <= middle) {
95+
array[k++] = array[i++];
96+
}
97+
98+
while (j <= right) {
99+
array[k++] = array[j++];
100+
}
101+
}
102+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.examplehub.sorts;
2+
3+
import com.examplehub.utils.RandomUtils;
4+
import com.examplehub.utils.SortUtils;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.util.Arrays;
9+
10+
import static org.junit.jupiter.api.Assertions.*;
11+
12+
class MergeSortTest {
13+
private Sort sort;
14+
15+
@BeforeEach
16+
public void before() {
17+
sort = new MergeSort();
18+
}
19+
20+
@Test
21+
void testSort() {
22+
int[] ints = RandomUtils.randomInts(-50, 50, 100);
23+
sort.sort(ints);
24+
assertTrue(SortUtils.isSorted(ints));
25+
}
26+
27+
@Test
28+
void sortInteger() {
29+
Integer[] integers = Arrays.stream(RandomUtils.randomInts(-50, 50, 100))
30+
.boxed().toArray(Integer[]::new);
31+
sort.sort(integers);
32+
assertTrue(SortUtils.isSorted(integers));
33+
}
34+
}

0 commit comments

Comments
 (0)