ajout crypto
This commit is contained in:
55
DEV3.2/ancien_controle/GeneriqueBulles.java
Normal file
55
DEV3.2/ancien_controle/GeneriqueBulles.java
Normal file
@@ -0,0 +1,55 @@
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
public class GeneriqueBulles<T extends Comparable<T>> {
|
||||
|
||||
// Méthode bulle générique
|
||||
public static <T extends Comparable<T>> boolean bulle(Queue<T> source, Queue<T> destination) {
|
||||
boolean changed = false;
|
||||
if (source.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
T prev = source.poll();
|
||||
while (!source.isEmpty()) {
|
||||
T current = source.poll();
|
||||
if (prev.compareTo(current) > 0) {
|
||||
changed = true;
|
||||
destination.add(current);
|
||||
prev = prev;
|
||||
} else {
|
||||
destination.add(prev);
|
||||
prev = current;
|
||||
}
|
||||
}
|
||||
destination.add(prev);
|
||||
return changed;
|
||||
}
|
||||
|
||||
// Méthode tri générique
|
||||
public static <T extends Comparable<T>> void tri(Queue<T> file) {
|
||||
Queue<T> intermediaire = new LinkedList<>();
|
||||
boolean changed;
|
||||
do {
|
||||
changed = bulle(file, intermediaire);
|
||||
Queue<T> temp = file;
|
||||
file = intermediaire;
|
||||
intermediaire = temp;
|
||||
} while (changed);
|
||||
|
||||
while (!file.isEmpty()) {
|
||||
System.out.print(file.poll() + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Queue<String> file = new LinkedList<>();
|
||||
file.add("Banane");
|
||||
file.add("Pomme");
|
||||
file.add("Orange");
|
||||
file.add("Ananas");
|
||||
|
||||
tri(file);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user