|
| 1 | + |
1 | 2 | // Import Arrays |
2 | 3 | import java.util.Arrays; |
3 | 4 | import java.util.Random; |
4 | 5 |
|
5 | 6 | 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; |
10 | 11 |
|
11 | 12 | public static void main(String[] args) { |
12 | 13 | // write your code here |
13 | 14 | // Clear console |
14 | 15 | System.out.print("\033\033"); |
15 | | - |
| 16 | + |
16 | 17 | Random rand = new Random(); |
17 | 18 |
|
18 | 19 | // 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) }; |
20 | 22 | // int[] tableau = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; |
21 | 23 | System.out.println(Arrays.toString(triBulle(tableau))); |
22 | | - |
| 24 | + |
23 | 25 | 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"); |
26 | 29 | } |
27 | 30 |
|
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 | + |
31 | 34 | boolean echange = true; |
32 | 35 | while (echange) { |
33 | 36 | echange = false; |
34 | 37 | 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 | + |
41 | 43 | echange = true; |
| 44 | + |
| 45 | + echanges++; |
| 46 | + comparaisons++; |
| 47 | + affectations += 3; |
42 | 48 | } |
| 49 | + |
| 50 | + comparaisons++; |
| 51 | + affectations++; |
43 | 52 | } |
| 53 | + |
| 54 | + comparaisons++; |
44 | 55 | } |
45 | | - |
46 | | - return tableau; |
| 56 | + |
| 57 | + return tab; |
47 | 58 | } |
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) { |
50 | 61 | 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 }; |
53 | 69 | triBulle(Tab); |
54 | 70 | } |
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); |
57 | 78 | } 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); |
67 | 83 | } |
68 | | - } |
| 84 | + } |
69 | 85 | } |
0 commit comments