-
Notifications
You must be signed in to change notification settings - Fork 0
/
InsertSorter.java
79 lines (67 loc) · 1.73 KB
/
InsertSorter.java
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
public class InsertSorter<T extends Comparable<? super T>> implements Sorter<List<T>, T>, Collector<T, List<T>, List<T>> {
private Comparator<T> comparator;
public InsertSorter(Comparator<T> comp) {
this.comparator=comp;
}
@Override
public List<T> getSorted(List<T> source) {
return sorted(source);
}
private void addItem(List<T> sorted,T item) {
// if(!sorted.isEmpty()) {
boolean added=false;
ListIterator<T> i=sorted.listIterator();
while(i.hasNext()) {
T next = i.next();
if(comparator.compare(next, item)>0) {
i.previous();
i.add(item);
added=true;
break;
}
}
if(!added) {
i.add(item);
}
// }else {
// sorted.add(item);
// }
}
private List<T> sorted(List<T> source) {
List<T> result=new LinkedList<T>();
for(T item:source) {
addItem(result,item);
}
return result;
}
@Override
public Supplier<List<T>> supplier() {
return LinkedList<T>::new;
}
@Override
public BiConsumer<List<T>, T> accumulator() {
return ( list, item ) -> addItem(list, item);
}
@Override
public BinaryOperator<List<T>> combiner() {
return ( left, right )->{ left.addAll(right); return left; };
}
@Override
public Set<Characteristics> characteristics() {
return Set.of(Characteristics.IDENTITY_FINISH);
}
@Override
public Function<List<T>, List<T>> finisher() {
return null;
}
}