Update
This commit is contained in:
11
DEV 3.2/TP05/Fusion/DoubleSorter.java
Normal file
11
DEV 3.2/TP05/Fusion/DoubleSorter.java
Normal file
@@ -0,0 +1,11 @@
|
||||
import java.util.Comparator;
|
||||
|
||||
public class DoubleSorter implements Comparator<Double> {
|
||||
|
||||
@Override
|
||||
public int compare(Double o1, Double o2) {
|
||||
if (o1 > o2) return 1;
|
||||
else if (o1 == o2) return 0;
|
||||
else return -1;
|
||||
}
|
||||
}
|
||||
44
DEV 3.2/TP05/Fusion/Fusion.java
Normal file
44
DEV 3.2/TP05/Fusion/Fusion.java
Normal file
@@ -0,0 +1,44 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
29
DEV 3.2/TP05/Fusion/Main.java
Normal file
29
DEV 3.2/TP05/Fusion/Main.java
Normal file
@@ -0,0 +1,29 @@
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Comparator;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Fusion<Double> f = new Fusion<Double>(new Comparator<Double>() {
|
||||
|
||||
@Override
|
||||
public int compare(Double o1, Double o2) {
|
||||
if (o1 > o2) return 1;
|
||||
else if (o1 == o2) return 0;
|
||||
else return -1;
|
||||
}
|
||||
});
|
||||
|
||||
ArrayDeque<Double> list = new ArrayDeque<>();
|
||||
|
||||
for (String arg : args) {
|
||||
try {
|
||||
double d = Double.parseDouble(arg);
|
||||
list.addFirst(d);
|
||||
} catch (Exception e) {
|
||||
System.out.println("Invalid arg : " + arg);
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(list.toString());
|
||||
}
|
||||
}
|
||||
4
DEV 3.2/TP05/Fusion/Sorter.java
Normal file
4
DEV 3.2/TP05/Fusion/Sorter.java
Normal file
@@ -0,0 +1,4 @@
|
||||
public interface Sorter<T> {
|
||||
|
||||
public boolean isFirstSecond(T a, T b);
|
||||
}
|
||||
Reference in New Issue
Block a user