forked from krimanisha/Hacktoberfest21
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStalinSort.java
More file actions
29 lines (23 loc) · 718 Bytes
/
StalinSort.java
File metadata and controls
29 lines (23 loc) · 718 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static java.util.Collections.emptyList;
public class StalinSort {
public static <T extends Comparable<T>> List<T> stalinSort(List<T> list) {
if (list.isEmpty()) {
return emptyList();
}
List<T> sorted = new ArrayList<>();
T max = null;
for (T candidate : list) {
if (sorted.isEmpty() || candidate.compareTo(max) >= 0) {
sorted.add(candidate);
max = candidate;
}
}
return sorted;
}
public static void main(String[] args) {
System.out.println(stalinSort(Arrays.asList(args)));
}
}