Skip to content

Commit 1f71951

Browse files
authored
Merge pull request #11 from CodingFactory-Repos/Feature/Optimisation
📊 - Update Stats
2 parents 14d916d + f0d510f commit 1f71951

3 files changed

Lines changed: 93 additions & 73 deletions

File tree

Tri-a-bulle-normal/src/Main.java

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,25 @@
22
import java.util.Random;
33

44
public class Main {
5-
public static float echanges = 0;
6-
public static float comparaisons = 0;
7-
public static float affectations = 0;
5+
public static float echanges = 0;
6+
public static float comparaisons = 0;
7+
public static float affectations = 0;
88

99
public static void main(String[] args) {
1010
// write your code here
1111
// Clear console
1212
System.out.print("\033[H\033[2J");
1313

1414
// Tableau de test
15-
// int[] tab = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
15+
// int[] tab = { 4, 3, 2, 1};
1616

1717
// System.out.println("Tableau de test : " + Arrays.toString(tri_bulleN(tab)));
18-
19-
stat(10,20,5,10);
2018

19+
stat(10, 20, 5, 1);
2120

2221
}
2322

2423
public static int[] tri_bulleN(int[] tab) {
25-
echanges = 0;
26-
comparaisons = 0;
27-
affectations = 0;
2824
int taille = tab.length;
2925
int tmp = 0;
3026
for (int i = 0; i < taille; i++) {
@@ -35,34 +31,40 @@ public static int[] tri_bulleN(int[] tab) {
3531
tab[j - 1] = tab[j];
3632
tab[j] = tmp;
3733

38-
echanges++;
34+
echanges++;
3935
comparaisons++;
4036
affectations += 3;
4137
}
4238
comparaisons++;
39+
affectations++;
4340
}
44-
comparaisons++;
45-
}
46-
47-
48-
//System.out.println("Il y a eu " + comparaisons + " comparaisons, " + echanges + " échange et " + affectations + " affectations");
41+
42+
affectations++;
43+
comparaisons++;
44+
}
45+
46+
// System.out.println("Il y a eu " + comparaisons + " comparaisons, " + echanges + " échange et " + affectations + " affectations");
4947
return tab;
5048
}
5149

52-
public static void stat(int min,int max, int step, int nbr) {
53-
int i;
54-
comparaisons = 0;
55-
echanges = 0;
56-
affectations = 0;
57-
for ( i = 1 ; i <= nbr ; i++) {
58-
int Tab[] =new Random().ints(min).toArray();
59-
tri_bulleN(Tab);
50+
public static void stat(int min, int max, int step, int nbr) {
51+
int i;
52+
comparaisons = 0;
53+
echanges = 0;
54+
affectations = 0;
55+
for (i = 1; i <= nbr; i++) {
56+
int Tab[] = new Random().ints(min).toArray();
57+
// int Tab[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
58+
// int Tab[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
59+
tri_bulleN(Tab);
60+
}
61+
if (min < max && i > nbr) {
62+
System.out.println("Le nombre d'affectations pour " + min + " est de " + affectations / nbr);
63+
stat((min + step), max, step, nbr);
64+
} else {
65+
System.out.println("Le nombre d'affectations pour " + min + " est de " + affectations / nbr);
66+
System.out.println("Le nombre de comparaisons est de " + comparaisons / nbr);
67+
System.out.println("Le nombre d'echanges est de " + echanges / nbr);
68+
}
6069
}
61-
if (min < max && i>nbr) {
62-
System.out.println("Le nombre d'affectations pour " + min + " est de " + affectations/nbr);
63-
stat((min+step),max,step,nbr);
64-
} else {
65-
System.out.println("Le nombre d'affectations pour " + min + " est de " + affectations/nbr);
66-
}
67-
}
6870
}
Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,85 @@
1+
12
// Import Arrays
23
import java.util.Arrays;
34
import java.util.Random;
45

56
public class Iteratif {
6-
7-
public static float comparaisons;
8-
public static float echanges;
9-
public static float affectations;
7+
8+
public static float comparaisons;
9+
public static float echanges;
10+
public static float affectations;
1011

1112
public static void main(String[] args) {
1213
// write your code here
1314
// Clear console
1415
System.out.print("\033\033");
15-
16+
1617
Random rand = new Random();
1718

1819
// Tableau de test
19-
int[] tableau = { rand.nextInt(100), rand.nextInt(100), rand.nextInt(100), rand.nextInt(100), rand.nextInt(100), rand.nextInt(100), rand.nextInt(100), rand.nextInt(100), rand.nextInt(100), rand.nextInt(100) };
20+
int[] tableau = { rand.nextInt(100), rand.nextInt(100), rand.nextInt(100), rand.nextInt(100), rand.nextInt(100),
21+
rand.nextInt(100), rand.nextInt(100), rand.nextInt(100), rand.nextInt(100), rand.nextInt(100) };
2022
// int[] tableau = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
2123
System.out.println(Arrays.toString(triBulle(tableau)));
22-
24+
2325
stat(10, 20, 5, 10);
24-
25-
// System.out.println("Résultat de " + comparaisons + " comparaisons, " + echanges + " échanges et " + (echanges * 3) + " affectations");
26+
27+
// System.out.println("Résultat de " + comparaisons + " comparaisons, " +
28+
// echanges + " échanges et " + (echanges * 3) + " affectations");
2629
}
2730

28-
public static int[] triBulle(int [] tableau) {
29-
int n = tableau.length;
30-
31+
public static int[] triBulle(int[] tab) {
32+
int n = tab.length;
33+
3134
boolean echange = true;
3235
while (echange) {
3336
echange = false;
3437
for (int i = 0; i < n - 1; i++) {
35-
comparaisons++;
36-
if (tableau[i] > tableau[i + 1]) {
37-
echanges++;
38-
int temp = tableau[i];
39-
tableau[i] = tableau[i + 1];
40-
tableau[i + 1] = temp;
38+
if (tab[i] > tab[i + 1]) {
39+
int temp = tab[i];
40+
tab[i] = tab[i + 1];
41+
tab[i + 1] = temp;
42+
4143
echange = true;
44+
45+
echanges++;
46+
comparaisons++;
47+
affectations += 3;
4248
}
49+
50+
comparaisons++;
51+
affectations++;
4352
}
53+
54+
comparaisons++;
4455
}
45-
46-
return tableau;
56+
57+
return tab;
4758
}
48-
49-
public static void stat(int min,int max, int step, int nbr) {
59+
60+
public static void stat(int min, int max, int step, int nbr) {
5061
int i;
51-
for ( i = 1 ; i <= nbr ; i++) {
52-
int Tab[] =new Random().ints(min).toArray();
62+
comparaisons = 0;
63+
echanges = 0;
64+
affectations = 0;
65+
for (i = 1; i <= nbr; i++) {
66+
int Tab[] = new Random().ints(min).toArray();
67+
// int Tab[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
68+
// int Tab[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
5369
triBulle(Tab);
5470
}
55-
if (min < max && i>nbr) {
56-
stat((min+step),max,step,nbr);
71+
// System.out.println("Le nombre de comparaisons est de " + comparaisons/nbr);
72+
73+
// System.out.println("Le nombre d'échanges est de " + echanges/nbr);
74+
75+
if (min < max && i > nbr) {
76+
System.out.println("Le nombre d'affectations pour " + min + " est de " + affectations / nbr);
77+
stat((min + step), max, step, nbr);
5778
} else {
58-
System.out.println("Le nombre de comparaisons est de " + comparaisons/nbr);
59-
System.out.println("Le nombre d'échanges est de " + echanges/nbr);
60-
61-
if (min < max && i>nbr) {
62-
System.out.println("Le nombre d'affectations pour " + min + " est de " + (echanges * 3)/nbr);
63-
stat((min+step),max,step,nbr);
64-
} else {
65-
System.out.println("Le nombre d'affectations pour " + min + " est de " + (echanges * 3)/nbr);
66-
}
79+
System.out.println("Le nombre d'affectations pour " + min + " est de " + affectations / nbr);
80+
81+
System.out.println("Le nombre de comparaisons est de " + comparaisons / nbr);
82+
System.out.println("Le nombre d'echanges est de " + echanges / nbr);
6783
}
68-
}
84+
}
6985
}

Tri-de-selection/src/Iteratif.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ public static void main(String[] args) {
5151
// int[] tableau = {1,2,3,4,5,6,7,8,9,10};
5252
int[] tableau = { rand.nextInt(100), rand.nextInt(100), rand.nextInt(100), rand.nextInt(100), rand.nextInt(100), rand.nextInt(100), rand.nextInt(100), rand.nextInt(100), rand.nextInt(100), rand.nextInt(100) };
5353

54-
System.out.println("Tableau d'avant fonction : " + Arrays.toString(tableau));
55-
System.out.println("Tableau après fonction : " + Arrays.toString(selectedSort(tableau)));
56-
System.out.println("Comparaisons : " + comparaisons);
57-
System.out.println("Echanges : " + echanges);
58-
System.out.println("Affectations : " + affectations );
59-
System.out.println("Opérations" + (echanges + affectations + comparaisons));
54+
// System.out.println("Tableau d'avant fonction : " + Arrays.toString(tableau));
55+
// System.out.println("Tableau après fonction : " + Arrays.toString(selectedSort(tableau)));
56+
// System.out.println("Comparaisons : " + comparaisons);
57+
// System.out.println("Echanges : " + echanges);
58+
// System.out.println("Affectations : " + affectations );
59+
// System.out.println("Opérations" + (echanges + affectations + comparaisons));
6060

61-
// stat(10,20,5,10);
61+
stat(10,20,5,10);
6262

6363
}
6464

@@ -78,6 +78,8 @@ public static void stat(int min,int max, int step, int nbr) {
7878
stat((min+step),max,step,nbr);
7979
} else {
8080
System.out.println("Le nombre d'affectations pour " + min + " est de " + affectations/nbr);
81+
System.out.println("Le nombre de comparaisons est de " + comparaisons/nbr);
82+
System.out.println("Le nombre d'echanges est de " + echanges/nbr);
8183
}
8284
}
8385
}

0 commit comments

Comments
 (0)