Skip to content

Commit af8d9ee

Browse files
added merge sort iterative algorithm
1 parent d2de265 commit af8d9ee

File tree

3 files changed

+156
-1
lines changed

3 files changed

+156
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ gradle clean assemble
3535
- [Insertion Sort](https://github.com/marioluan/java-sorting-algorithms/blob/master/src/main/java/io/github/marioluan/algorithms/sorting/InsertionSort.java)
3636
- [Selection Sort](https://github.com/marioluan/java-sorting-algorithms/blob/master/src/main/java/io/github/marioluan/algorithms/sorting/SelectionSort.java)
3737
- [Shellsort](https://github.com/marioluan/java-sorting-algorithms/blob/master/src/main/java/io/github/marioluan/algorithms/sorting/ShellSort.java)
38-
- [Merge Sort](https://github.com/marioluan/java-sorting-algorithms/blob/master/src/main/java/io/github/marioluan/algorithms/sorting/MergeSort.java)
38+
- Merge Sort
39+
-- [Recursive](https://github.com/marioluan/java-sorting-algorithms/blob/master/src/main/java/io/github/marioluan/algorithms/sorting/MergeSortRecursive.java)
40+
-- [Iterative](https://github.com/marioluan/java-sorting-algorithms/blob/master/src/main/java/io/github/marioluan/algorithms/sorting/MergeSortIterative.java)
3941

4042
## References
4143
- [https://www.coursera.org/learn/algorithms-part1](https://www.coursera.org/learn/algorithms-part1)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package io.github.marioluan.algorithms.sorting;
2+
3+
/**
4+
* Iterative stable implementation of merge sort algorithm. <br>
5+
* <strong>Time complexity:</strong> O(N log N) <br>
6+
* <strong>Space complexity:</strong> O(n) <br>
7+
* References:
8+
* <ul>
9+
* <li><a>https://www.coursera.org/learn/algorithms-part1</a>.</li>
10+
* <li><a>https://upload.wikimedia.org/wikipedia/commons/c/cc/Merge-sort-example-300px.gif</a></li>
11+
* </ul>
12+
*
13+
* @author marioluan
14+
*/
15+
public final class MergeSortIterative extends Sortable {
16+
17+
private MergeSortIterative() {
18+
}
19+
20+
/**
21+
* Sort items from array a.
22+
*
23+
* @param a
24+
*/
25+
@SuppressWarnings("rawtypes")
26+
public static void sort(Comparable[] a) {
27+
Comparable[] aux = new Comparable[a.length];
28+
sort(a, aux);
29+
};
30+
31+
/**
32+
* Iteratively sort items from array a.
33+
*
34+
* @param a
35+
* @param aux
36+
* @param lo
37+
* @param hi
38+
*/
39+
@SuppressWarnings("rawtypes")
40+
private static void sort(Comparable[] a, Comparable[] aux) {
41+
int n = a.length;
42+
for (int size = 1; size < n; size = size + size)
43+
for (int lo = 0; lo < n - size; lo += size + size)
44+
merge(a, aux, lo, lo + size - 1,
45+
Math.min(lo + size + size - 1, n - 1));
46+
}
47+
48+
/**
49+
* Merge sorted sub-arrays a and aux.
50+
*
51+
* @param a
52+
* @param aux
53+
* @param lo
54+
* @param mid
55+
* @param hi
56+
*/
57+
@SuppressWarnings("rawtypes")
58+
private static void merge(Comparable[] a, Comparable[] aux, int lo, int mid,
59+
int hi) {
60+
copy(a, aux, lo, hi);
61+
int i = lo;
62+
int j = mid + 1;
63+
64+
for (int k = lo; k <= hi; k++) {
65+
if (i > mid)
66+
a[k] = aux[j++];
67+
else if (j > hi)
68+
a[k] = aux[i++];
69+
else if (lessOrEqual(aux[j], aux[i]))
70+
a[k] = aux[j++];
71+
else
72+
a[k] = aux[i++];
73+
}
74+
}
75+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package io.github.marioluan.algorithms.sorting;
2+
3+
import static org.junit.Assert.assertArrayEquals;
4+
import static com.greghaskins.spectrum.Spectrum.describe;
5+
import static com.greghaskins.spectrum.Spectrum.beforeEach;
6+
import static com.greghaskins.spectrum.Spectrum.it;
7+
8+
import java.util.Arrays;
9+
import java.util.Random;
10+
11+
import org.junit.runner.RunWith;
12+
13+
import com.greghaskins.spectrum.Spectrum;
14+
15+
import io.github.marioluan.algorithms.test.support.SupportHelper;
16+
17+
@SuppressWarnings("rawtypes")
18+
@RunWith(Spectrum.class)
19+
public class MergeSortIterativeTest {
20+
21+
private static final Random RANDOM = new Random();
22+
private Comparable[] array = null;
23+
private Comparable[] clone = null;
24+
private int n = 0;
25+
26+
{
27+
describe("MergeSortIterative", () -> {
28+
describe(".sort", () -> {
29+
30+
describe("when array is small", () -> {
31+
beforeEach(() -> {
32+
n = RANDOM.nextInt(7) + 1;
33+
array = SupportHelper.buildRandomArray(n);
34+
clone = array.clone();
35+
});
36+
37+
it("sorts it", () -> {
38+
Arrays.sort(clone);
39+
MergeSortIterative.sort(array);
40+
41+
assertArrayEquals(array, clone);
42+
});
43+
});
44+
45+
describe("when array is huge", () -> {
46+
beforeEach(() -> {
47+
n = RANDOM.nextInt((int) Math.pow(10, 5)) + 1;
48+
array = SupportHelper.buildRandomArray(n);
49+
clone = array.clone();
50+
});
51+
52+
it("sorts it", () -> {
53+
Arrays.sort(clone);
54+
MergeSortIterative.sort(array);
55+
56+
assertArrayEquals(array, clone);
57+
});
58+
});
59+
60+
describe("when array is partially sorted", () -> {
61+
beforeEach(() -> {
62+
array = new Comparable[] { 1, 2, 3, 6, 9, 5, 10, 4, 11,
63+
13, 14, 15, 16, 17, 18, 19, 20 };
64+
clone = array.clone();
65+
});
66+
67+
it("sorts it", () -> {
68+
Arrays.sort(clone);
69+
MergeSortIterative.sort(array);
70+
71+
assertArrayEquals(array, clone);
72+
});
73+
});
74+
75+
});
76+
});
77+
}
78+
}

0 commit comments

Comments
 (0)