Skip to content

Commit 6acf621

Browse files
authored
Update StalinSort.java
1 parent 9722f7f commit 6acf621

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed
Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package com.thealgorithms.sorts;
22

3-
/**
4-
* @author Anant Jain (https://github.com/anant-jain01)
5-
* @see https://medium.com/@kaweendra/the-ultimate-sorting-algorithm-6513d6968420
6-
*/
7-
public class StalinSort implements SortAlgorithm {
3+
import java.util.Arrays;
84

5+
public class StalinSort implements SortAlgorithm {
96
public <T extends Comparable<T>> T[] sort(T[] array) {
7+
validateInput(array);
108
int currentIndex = 0;
119

1210
for (int i = 1; i < array.length; i++) {
@@ -16,26 +14,23 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
1614
}
1715
}
1816

19-
// Create a result array with sorted elements
2017
T[] result = (T[]) new Comparable[currentIndex + 1];
2118
System.arraycopy(array, 0, result, 0, currentIndex + 1);
22-
2319
return result;
2420
}
2521

26-
// Driver Program
22+
private void validateInput(final Comparable<?>[] array) {
23+
if (array.length == 0) {
24+
throw new IllegalArgumentException("Input array must not be empty.");
25+
}
26+
}
27+
2728
public static void main(String[] args) {
28-
// Integer Input
2929
Integer[] integers = {4, 23, 6, 78, 1, 54, 231, 9, 12};
30-
3130
StalinSort stalinSort = new StalinSort();
32-
33-
// print a sorted array
3431
SortUtils.print(stalinSort.sort(integers));
35-
36-
// String Input
32+
3733
String[] strings = {"c", "a", "e", "b", "d"};
38-
3934
SortUtils.print(stalinSort.sort(strings));
4035
}
4136
}

0 commit comments

Comments
 (0)