44 lines
991 B
Java
44 lines
991 B
Java
import java.util.ArrayDeque;
|
|
import java.util.Comparator;
|
|
|
|
public class Fusion<T> {
|
|
|
|
private Comparator<T> s;
|
|
|
|
public Fusion(Comparator<T> s) {
|
|
this.s = s;
|
|
}
|
|
|
|
public void scinder(ArrayDeque<T> first, ArrayDeque<T> second) {
|
|
second.clear();
|
|
|
|
int count = first.size() / 2;
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
second.addFirst(first.pollLast());
|
|
}
|
|
}
|
|
|
|
public void fusionner(ArrayDeque<T> first, ArrayDeque<T> second) {
|
|
for (T e : second) {
|
|
first.addFirst(e);
|
|
}
|
|
}
|
|
|
|
public void trier(ArrayDeque<T> d) {
|
|
switch (d.size()) {
|
|
case 2:
|
|
if (s.compare(d.getFirst(), d.getLast()) < 0) {
|
|
d.addFirst(d.pollLast());
|
|
}
|
|
break;
|
|
default:
|
|
ArrayDeque<T> second = new ArrayDeque<>();
|
|
scinder(d, second);
|
|
|
|
trier(d);
|
|
trier(second);
|
|
fusionner(d, second);
|
|
}
|
|
}
|
|
} |