33import java .util .Random ;
44
55public class Iteratif {
6+
7+ public static float comparaisons ;
8+ public static float echanges ;
9+ public static float affectations ;
610
711 public static void main (String [] args ) {
812 // write your code here
@@ -12,18 +16,25 @@ public static void main(String[] args) {
1216 Random rand = new Random ();
1317
1418 // Tableau de test
15- 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 ) };
16-
17- System .out .println (Arrays .toString (triBulle (tableau )));
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 = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };
21+ // System.out.println(Arrays.toString(triBulle(tableau)));
22+
23+ stat (10 , 20 , 5 , 10 );
24+
25+ // System.out.println("Résultat de " + comparaisons + " comparaisons, " + echanges + " échanges et " + (echanges * 3) + " affectations");
1826 }
1927
2028 public static int [] triBulle (int [] tableau ) {
2129 int n = tableau .length ;
30+
2231 boolean echange = true ;
2332 while (echange ) {
2433 echange = false ;
2534 for (int i = 0 ; i < n - 1 ; i ++) {
35+ comparaisons ++;
2636 if (tableau [i ] > tableau [i + 1 ]) {
37+ echanges ++;
2738 int temp = tableau [i ];
2839 tableau [i ] = tableau [i + 1 ];
2940 tableau [i + 1 ] = temp ;
@@ -34,4 +45,25 @@ public static int[] triBulle(int [] tableau) {
3445
3546 return tableau ;
3647 }
48+
49+ public static void stat (int min ,int max , int step , int nbr ) {
50+ int i ;
51+ for ( i = 1 ; i <= nbr ; i ++) {
52+ int Tab [] =new Random ().ints (min ).toArray ();
53+ triBulle (Tab );
54+ }
55+ if (min < max && i >nbr ) {
56+ stat ((min +step ),max ,step ,nbr );
57+ } 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+ }
67+ }
68+ }
3769}
0 commit comments