From 3a28323d25ed85d3544cccf049ef55b8473d6c62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Heredero?= Date: Mon, 14 Mar 2022 15:07:48 +0100 Subject: [PATCH] first part lab 12 --- bin/lab12_sort/ArrayFactory.class | Bin 0 -> 983 bytes bin/lab12_sort/BubbleSort.class | Bin 0 -> 561 bytes bin/lab12_sort/FusionSort.class | Bin 0 -> 1220 bytes bin/lab12_sort/SelectionSort.class | Bin 0 -> 611 bytes bin/lab12_sort/SortApplication.class | Bin 0 -> 3799 bytes bin/tools/Chrono.class | Bin 0 -> 1272 bytes src/lab12_sort/ArrayFactory.java | 34 +++++++++ src/lab12_sort/BubbleSort.java | 19 +++++ src/lab12_sort/FusionSort.java | 68 +++++++++++++++++ src/lab12_sort/SelectionSort.java | 20 +++++ src/lab12_sort/SortApplication.java | 107 +++++++++++++++++++++++++++ src/tools/Chrono.java | 38 ++++++++++ 12 files changed, 286 insertions(+) create mode 100644 bin/lab12_sort/ArrayFactory.class create mode 100644 bin/lab12_sort/BubbleSort.class create mode 100644 bin/lab12_sort/FusionSort.class create mode 100644 bin/lab12_sort/SelectionSort.class create mode 100644 bin/lab12_sort/SortApplication.class create mode 100644 bin/tools/Chrono.class create mode 100644 src/lab12_sort/ArrayFactory.java create mode 100644 src/lab12_sort/BubbleSort.java create mode 100644 src/lab12_sort/FusionSort.java create mode 100644 src/lab12_sort/SelectionSort.java create mode 100644 src/lab12_sort/SortApplication.java create mode 100644 src/tools/Chrono.java diff --git a/bin/lab12_sort/ArrayFactory.class b/bin/lab12_sort/ArrayFactory.class new file mode 100644 index 0000000000000000000000000000000000000000..eefd7c4be42e6fb23a9676fcfa73e24852a55c63 GIT binary patch literal 983 zcmZ{iOHUI~6vzK}ddHzdOCR#E?I15vYQaWhbm1d0CL~P?O{^g?EX-|ZkinrP!;}DP zzJ#uH#a1_xsNn~`GXsX$_(ye1%@cWuW+0?8C~ zToK6B-Hx-`+ip4jOV#omfqcE&R^FQO-7t=mGT3x?1q${5w0|U!Yx|B0oENHNcef)n zc3-H~%Ih@)xpTN);Cj`5jfpIh;W3)R7{&#Rj}e=UAuX(Ey@vH>U|DVIVLd1gzr^Nhll_xrM#z%@o}fC4>k9CIkLQe#~S7AC2e z!8oq-R$_{kX=rE28<;kb6~mOD!E8)>$dYgc7HR8=C&w76SVha0cH>ABR)vVaE0>7l z5ki)4auHyTwusRPZ@RA&<4HCTq4+=XK19(X6cgGXB#9zSa?D+DyrXi+v2@NcW$8Qb zk*Zjh?lq2)CYH46UxNJ{0Gp*4qInAhD{}r4|4)mVNwz4Ug6RRsr3=XN1?2nyWC}8d O97$>%;Rdxe%>M&Q!lqmR literal 0 HcmV?d00001 diff --git a/bin/lab12_sort/BubbleSort.class b/bin/lab12_sort/BubbleSort.class new file mode 100644 index 0000000000000000000000000000000000000000..9ff8c9ed0c77236e6f88ac16250ec787996b16ad GIT binary patch literal 561 zcmZuu&r2Io5dLO=WKC)s8;xn3v{0mAR!lI6g03Da6ar~;NXeld>f2_>6s z!^ro$L6e~aOE7bwj#Q;4&P*Bt%u_lcWb5Hn!r|3q>crK2F`Gw z*`OnkuRI|SSu;Ky2-3}1wSRn4gQrQUW`7j5gO8!slKHn_S?^}O1MB!R0D}=Xpmm5U z^g?gD*OtqZ(&i)FWH?Um1w zXSnSa$Ecz0n}+F~7*cd^tr) zERkR1oejJg>vJa>Fhi`@+dvkxRG7pv=8z*(U~Zm~$zGs(`yRI3^A+D3cN`;lIWBzl E2y264Hvj+t literal 0 HcmV?d00001 diff --git a/bin/lab12_sort/FusionSort.class b/bin/lab12_sort/FusionSort.class new file mode 100644 index 0000000000000000000000000000000000000000..d7a441c27aad6123ed496f03bdbc05619b20c6cd GIT binary patch literal 1220 zcmZuwU279T6g`vOO?KC`X&TqsZc-bq{a7`{529ABpi(JRD?|dNg4r}tx2B1tiRhyc z^cM)eR)xHYeNhoD`ef;EQU8I~Gn3LPxG;0?o%?m}nc2k3!EwZwH_pext#7w=#5 z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h z!(A%AsUlGR<))lxceQ)PLPfBO$`9cbbzYC>LntaD)KgJ?_OMI?VZeYpa}b>n|P7Hw?^SoX%t?iWQ`n}Z*xlONGw*rMs@uh zzE_q8ug&4o!dsx$if|Y!iSunZf^Nid5J#=ILL=idy47(Kq-0O!t7HJL;|*(958zF9 z&cVEYh*mpqv7UC)@=D|Fj^J#c9PVQ3GErtyDGI)2Pj{bV*DRtHXB*dTUFP(Ui>GHC zVqN3ys^qo#KUdnUsY)(q_0ysii`K-2u&@YP2u$EOAK(N*ZSd7I!bOlk3zFD}6pN9- zD8>l5aSptRS8=kUG*>wA=V|Z2CH&kfjSDZ~9ji1^{g&?+Dp{QsTP!aq{NRgNljJ_>pg~esJX}1{v0*3H2 zd@|8QKB9@QCjKB_G{$?U8(gWzhjw=E%sKbmbLQ*MkDmb)abJZZpn0xq*9!}qHP>-f zgax{`j2)w38_q^yd2P$Acmi#8KV76n44vrZMy^$c4e>=@goK(h23RQ76?voRwO znYA3tyDgB;wjSqJ3B2I0n{kBDsi7TR#GTEp#z1r$Vu;6)LXV2{ACk(i>Yll+p%;Au zQO9uH6^j)RbdjM08rl#M=qy=|`KYnIX4Y1WHCt+QD~7#l)GXfn#jv+&)ddozpbexw zT=(26^9^fI5sA$0&yi^$SppX{M7c%ObDOgYq_Vlz5u?Zn5IQf=d)l7)hGnmtwK&F6 zP%-i6h|)j}lejFP?ihB%Tz)Pv7Id+h%*FBQH4WEMq%4eTRR%YhU2FyU zYGxR5Q^Oc8MnNWoGL15qZZQevI385s$NX=yDCWq=%ycG34~IPND0@cb#gb9=t&f-8 zMy+B#usEFd<^!BypD;`&hsf+O6b_TV6b?bIvG*JIm+9%E5$bjZu~f`cD(zpgfZdIEVAJ2tb8lE^`jW($p=9g?1<640j?7($;<`k2J<*-%q%M%Ovic_d&C=Dnz1d4 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; + } +}