Skip to content

Commit d22fbb2

Browse files
added selection sort algorithm
1 parent 7d2ad27 commit d22fbb2

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.github.marioluan.algorithms.sorting;
2+
3+
/**
4+
* In-place, non-stable implementation of selection sort algorithm. <br>
5+
* <strong>Time complexity:</strong> O(N^2) <br>
6+
* <strong>Space complexity:</strong> O(1) <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/b/b0/Selection_sort_animation.gif</a></li>
11+
* </ul>
12+
*
13+
* @author marioluan
14+
*/
15+
public final class SelectionSort extends Sortable {
16+
17+
private SelectionSort() {
18+
}
19+
20+
/**
21+
* Sort the elements from array a.
22+
*
23+
* @param a
24+
*/
25+
@SuppressWarnings("rawtypes")
26+
public static void sort(Comparable[] a) {
27+
int n = a.length;
28+
29+
for (int i = 0; i < n; i++) {
30+
int min = findMin(a, i, n);
31+
swap(a, i, min);
32+
}
33+
}
34+
}
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 SelectionSortTest {
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("InsertionSort", () -> {
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+
SelectionSort.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+
SelectionSort.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+
SelectionSort.sort(array);
70+
71+
assertArrayEquals(array, clone);
72+
});
73+
});
74+
75+
});
76+
});
77+
}
78+
}

0 commit comments

Comments
 (0)