APL/DEV 3.2/TP05/Fusion/Fusion.java

44 lines
991 B
Java
Raw Normal View History

2022-11-17 16:20:15 +01:00
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);
}
}
}