diff --git a/bin/C10_Tri_et_complexite/C103_Algorithme_de_tri/Bubble.class b/bin/C10_Tri_et_complexite/C103_Algorithme_de_tri/Bubble.class new file mode 100644 index 0000000..037dd55 Binary files /dev/null and b/bin/C10_Tri_et_complexite/C103_Algorithme_de_tri/Bubble.class differ diff --git a/bin/C10_Tri_et_complexite/C103_Algorithme_de_tri/Selection.class b/bin/C10_Tri_et_complexite/C103_Algorithme_de_tri/Selection.class index 180e432..1d1133c 100644 Binary files a/bin/C10_Tri_et_complexite/C103_Algorithme_de_tri/Selection.class and b/bin/C10_Tri_et_complexite/C103_Algorithme_de_tri/Selection.class differ diff --git a/bin/C10_Tri_et_complexite/C103_Algorithme_de_tri/Short.class b/bin/C10_Tri_et_complexite/C103_Algorithme_de_tri/Short.class new file mode 100644 index 0000000..2868853 Binary files /dev/null and b/bin/C10_Tri_et_complexite/C103_Algorithme_de_tri/Short.class differ diff --git a/bin/tools/Chrono.class b/bin/tools/Chrono.class new file mode 100644 index 0000000..068f0df Binary files /dev/null and b/bin/tools/Chrono.class differ diff --git a/src/C10_Tri_et_complexite/C103_Algorithme_de_tri/Bubble.java b/src/C10_Tri_et_complexite/C103_Algorithme_de_tri/Bubble.java new file mode 100644 index 0000000..8fe22fb --- /dev/null +++ b/src/C10_Tri_et_complexite/C103_Algorithme_de_tri/Bubble.java @@ -0,0 +1,23 @@ +package C10_Tri_et_complexite.C103_Algorithme_de_tri; + +public class Bubble { + + Bubble(int[] a){ + Short.printList(a); + + boolean shorted = false; + while (shorted == false) { + shorted = true; + for (int i = 0; i < a.length-1; i++) { + if (a[i] > a[i+1]) { + int tmp = a[i]; + a[i] = a[i+1]; + a[i+1] = tmp; + shorted = false; + } + } + } + + Short.printList(a); + } +} diff --git a/src/C10_Tri_et_complexite/C103_Algorithme_de_tri/Selection.java b/src/C10_Tri_et_complexite/C103_Algorithme_de_tri/Selection.java index 4799e56..bb9869f 100644 --- a/src/C10_Tri_et_complexite/C103_Algorithme_de_tri/Selection.java +++ b/src/C10_Tri_et_complexite/C103_Algorithme_de_tri/Selection.java @@ -1,18 +1,10 @@ package C10_Tri_et_complexite.C103_Algorithme_de_tri; -import java.util.Random; - -// Tri d'un tableau; - public class Selection { - public static void main(String[] args) throws Exception { - new Selection(100); - } + Selection(int[] a){ + Short.printList(a); - Selection(int n){ - int[] a = newList(n); - printList(a); for (int i = 0; i < a.length-1; i++) { int min = a[i]; int iMin = i; @@ -26,22 +18,7 @@ public class Selection { a[i] = a[iMin]; a[iMin] = tmp; } - printList(a); - } - int[] newList(int size){ - int[] a = new int[size]; - Random r = new Random(); - for (int i = 0; i < a.length; i++) { - a[i] = r.nextInt(1000); - } - return a; - } - - static void printList(int[] a){ - for (int i : a) { - System.out.print(i + " "); - } - System.out.println("\n"); + Short.printList(a); } } diff --git a/src/C10_Tri_et_complexite/C103_Algorithme_de_tri/Short.java b/src/C10_Tri_et_complexite/C103_Algorithme_de_tri/Short.java new file mode 100644 index 0000000..dae4c71 --- /dev/null +++ b/src/C10_Tri_et_complexite/C103_Algorithme_de_tri/Short.java @@ -0,0 +1,46 @@ +package C10_Tri_et_complexite.C103_Algorithme_de_tri; + +import java.util.Random; + +import tools.Chrono; + +public class Short { + private static boolean print = false; + public static void main(String[] args) { + int[] a = newList(1000); + + + Chrono selection = new Chrono("Selection"); + new Selection(a.clone()); + selection.stop(); + + if(print) System.out.println("-----------\n"); + + Chrono bubble = new Chrono("Bubble"); + new Bubble(a.clone()); + bubble.stop(); + + System.out.println(selection); + System.out.println(bubble); + } + + private static int[] newList(int size){ + int[] a = new int[size]; + Random r = new Random(); + for (int i = 0; i < a.length; i++) { + a[i] = r.nextInt(1000); + } + return a; + } + + static void printList(int[] a){ + if (print) { + for (int i : a) { + System.out.print(i + " "); + } + System.out.println("\n"); + } + } + + +} diff --git a/src/tools/Chrono.java b/src/tools/Chrono.java new file mode 100644 index 0000000..a702d6f --- /dev/null +++ b/src/tools/Chrono.java @@ -0,0 +1,24 @@ +package tools; + +public class Chrono { + private long start, end, total; + private String name; + + public Chrono(String name){ + this.name = name; + start = System.nanoTime(); + } + + public void stop(){ + end = System.nanoTime(); + total = (long) ((end- start)/1000.0); + } + + public String toString(){ + String s = name; + s += " : \t"; + s += total; + s += " uS"; + return s; + } +}