diff --git a/bin/lab12_sort/ArrayFactory.class b/bin/lab12_sort/ArrayFactory.class new file mode 100644 index 0000000..eefd7c4 Binary files /dev/null and b/bin/lab12_sort/ArrayFactory.class differ diff --git a/bin/lab12_sort/BubbleSort.class b/bin/lab12_sort/BubbleSort.class new file mode 100644 index 0000000..9ff8c9e Binary files /dev/null and b/bin/lab12_sort/BubbleSort.class differ diff --git a/bin/lab12_sort/FusionSort.class b/bin/lab12_sort/FusionSort.class new file mode 100644 index 0000000..d7a441c Binary files /dev/null and b/bin/lab12_sort/FusionSort.class differ diff --git a/bin/lab12_sort/SelectionSort.class b/bin/lab12_sort/SelectionSort.class new file mode 100644 index 0000000..08dae46 Binary files /dev/null and b/bin/lab12_sort/SelectionSort.class differ diff --git a/bin/lab12_sort/SortApplication.class b/bin/lab12_sort/SortApplication.class new file mode 100644 index 0000000..afef124 Binary files /dev/null and b/bin/lab12_sort/SortApplication.class differ diff --git a/bin/tools/Chrono.class b/bin/tools/Chrono.class new file mode 100644 index 0000000..6e211fe Binary files /dev/null and b/bin/tools/Chrono.class differ diff --git a/src/lab12_sort/ArrayFactory.java b/src/lab12_sort/ArrayFactory.java new file mode 100644 index 0000000..f153501 --- /dev/null +++ b/src/lab12_sort/ArrayFactory.java @@ -0,0 +1,34 @@ +package lab12_sort; + +import java.util.Random; + +public abstract class ArrayFactory { + static int[] createRandomArray(int size, int maxValue){ + int[] array = new int[size]; + Random r = new Random(); + for (int i = 0; i < array.length; i++) { + array[i] = r.nextInt(maxValue); + } + return array; + } + + static int[] createInvertedSortedArray(int size){ + int[] array = new int[size]; + for (int i = 0; i < array.length; i++) { + array[i] = size-i-1; + } + return array; + } + + static int[] createShuffleArray(int size){ + int[] array = new int[size]; + for (int i = 0; i < array.length; i++) { + if(i%2 == 0){ + array[i] = i/2; + } else { + array[i] = (size-1)-(i/2); + } + } + return array; + } +} diff --git a/src/lab12_sort/BubbleSort.java b/src/lab12_sort/BubbleSort.java new file mode 100644 index 0000000..50c5c0a --- /dev/null +++ b/src/lab12_sort/BubbleSort.java @@ -0,0 +1,19 @@ +package lab12_sort; + +public abstract class BubbleSort { + static public int[] sort(int[] array){ + boolean shorted = false; + while (shorted == false) { + shorted = true; + for (int i = 0; i < array.length-1; i++) { + if (array[i] > array[i+1]) { + int tmp = array[i]; + array[i] = array[i+1]; + array[i+1] = tmp; + shorted = false; + } + } + } + return array; + } +} diff --git a/src/lab12_sort/FusionSort.java b/src/lab12_sort/FusionSort.java new file mode 100644 index 0000000..cb3cfa6 --- /dev/null +++ b/src/lab12_sort/FusionSort.java @@ -0,0 +1,68 @@ +package lab12_sort; + +public abstract class FusionSort { + private static void fusion(int tab[], int debut, int milieu, int fin) { + int n1 = milieu - debut + 1; + int n2 = fin - milieu; + + int G[] = new int[n1]; + int D[] = new int[n2]; + + for (int i = 0; i < n1; i++) + G[i] = tab[debut + i]; + + for (int j = 0; j < n2; j++) + D[j] = tab[milieu + 1 + j]; + + // maintient trois pointeurs, un pour chacun des deux tableaux et un pour + // maintenir l'index actuel du tableau trié final + int i, j, k; + i = 0; + j = 0; + k = debut; + + while (i < n1 && j < n2) { + if (G[i] <= D[j]) { + tab[k] = G[i]; + i++; + } else { + tab[k] = D[j]; + j++; + } + k++; + } + + // Copiez tous les éléments restants du tableau non vide + while (i < n1) { + tab[k] = G[i]; + i++; + k++; + } + + while (j < n2) { + tab[k] = D[j]; + j++; + k++; + } + } + + // Tri par fusion + private static void triFusion(int tab[], int debut, int fin) { + + if (debut < fin) { + + // Trouvez le point milieu pour diviser le tableau en deux moitiés + int m = (debut + fin) / 2; + + triFusion(tab, debut, m); + triFusion(tab, m + 1, fin); + + // Fusionnez les deux moitiés triées + fusion(tab, debut, m, fin); + } + } + + static public void sort(int[] array){ + triFusion(array, 0, array.length - 1); + } +} diff --git a/src/lab12_sort/SelectionSort.java b/src/lab12_sort/SelectionSort.java new file mode 100644 index 0000000..9959448 --- /dev/null +++ b/src/lab12_sort/SelectionSort.java @@ -0,0 +1,20 @@ +package lab12_sort; + +public abstract class SelectionSort { + static public int[] sort(int[] array){ + for (int i = 0; i < array.length-1; i++) { + int min = array[i]; + int iMin = i; + for (int j = i; j < array.length; j++) { + if(array[j] < min){ + min = array[j]; + iMin = j; + } + } + int tmp = array[i]; + array[i] = array[iMin]; + array[iMin] = tmp; + } + return array; + } +} diff --git a/src/lab12_sort/SortApplication.java b/src/lab12_sort/SortApplication.java new file mode 100644 index 0000000..094f9af --- /dev/null +++ b/src/lab12_sort/SortApplication.java @@ -0,0 +1,107 @@ +package lab12_sort; + +import tools.Chrono; + +public class SortApplication { + public static void main(String[] args) { + long[][][] selection = new long[3][451][10]; + long[][][] bubble = new long[3][451][10]; + long[][][] yShort = new long[3][451][10]; + long[][][] fusion = new long[3][451][10]; + + for (int iValue = 450; iValue >= 0; iValue--) { + int value = iValue*200 + 10000; + //int value = iValue*10 + 100; + Chrono valueChrono = new Chrono(); + long sortTime = 0; + + for (int iAttempt = 0; iAttempt < 10; iAttempt++) { + for (int iTable = 0; iTable < 3; iTable++){ + int[] arrayIN; + switch (iTable) { + case 0: + arrayIN = ArrayFactory.createRandomArray(value, value); + break; + case 1: + arrayIN = ArrayFactory.createInvertedSortedArray(value); + break; + case 2: + arrayIN = ArrayFactory.createShuffleArray(value); + break; + + default: + arrayIN = null; + break; + } + Chrono sort = new Chrono(); + + Chrono selectionChrono = new Chrono(); + SelectionSort.sort(arrayIN); + selection[iTable][iValue][iAttempt] = selectionChrono.stop(); + + Chrono bubbleChrono = new Chrono(); + BubbleSort.sort(arrayIN); + bubble[iTable][iValue][iAttempt] = bubbleChrono.stop(); + + Chrono yShortChrono = new Chrono(); + YSort.sort(arrayIN); + yShort[iTable][iValue][iAttempt] = yShortChrono.stop(); + + Chrono fusionChrono = new Chrono(); + FusionSort.sort(arrayIN); + fusion[iTable][iValue][iAttempt] = fusionChrono.stop(); + + sortTime = sort.stop(); + } + } + + long[][] selectionAverage = new long[3][451]; + long[][] bubbleAverage = new long[3][451]; + long[][] yShortAverage = new long[3][451]; + long[][] fusionAverage = new long[3][451]; + + for(int iTable = 0; iTable < 3; iTable++){ + for(int j = 0; j < 10; j++){ + selectionAverage[iTable][iValue] += selection[iTable][iValue][j]; + bubbleAverage[iTable][iValue] += bubble[iTable][iValue][j]; + yShortAverage[iTable][iValue] += yShort[iTable][iValue][j]; + fusionAverage[iTable][iValue] += fusion[iTable][iValue][j]; + } + selectionAverage[iTable][iValue] /= 10.0; + bubbleAverage[iTable][iValue] /= 10.0; + yShortAverage[iTable][iValue] /= 10.0; + fusionAverage[iTable][iValue] /= 10.0; + + print(value, iTable, "Selection", selectionAverage[iTable][iValue]); + print(value, iTable, "Bubble ", bubbleAverage[iTable][iValue]); + print(value, iTable, "YShort ", yShortAverage[iTable][iValue]); + print(value, iTable, "Fusion ", fusionAverage[iTable][iValue]); + System.out.println(""); + } + System.out.println("Total time for sort " + value + ": " + sortTime/1000000.0 + " s"); + System.out.println("Total time for " + value + ": " + valueChrono.stop()/1000000.0 + " s"); + System.out.println("---------- ---------- ----------\n"); + } + } + + static void print(int value, int iTable, String name, long average){ + System.out.print(name + ""); + System.out.print("\tTable: " + iTable + " | Value: " + value); + System.out.print(" -> [us] " + average); + System.out.print("\n"); + } + + static void displayArray(int[] array){ + int size = array.length; + String s = "Size "; + s += size; + s += ": {"; + for (int i = 0; i < array.length-1; i++) { + s += array[i]; + s += ", "; + } + s += array[size-1]; + s += "}"; + System.out.println(s); + } +} diff --git a/src/tools/Chrono.java b/src/tools/Chrono.java new file mode 100644 index 0000000..b941c9b --- /dev/null +++ b/src/tools/Chrono.java @@ -0,0 +1,38 @@ +package tools; + +public class Chrono { + private long start, end, total; + private String name = null; + + public Chrono(String name){ + this.name = name; + start = System.nanoTime(); + } + public Chrono(){ + start = System.nanoTime(); + } + + /** + * Stop the chrono and return the total time in us + * @return the total length in us + */ + public long stop(){ + end = System.nanoTime(); + total = (long) ((end - start)/1000.0); + return total; + } + + public String toString(){ + String s = ""; + if (name == null){ + s += total; + s += " uS"; + } else { + s += name; + s += " : \t"; + s += total; + s += " uS"; + } + return s; + } +}