1- import java .util .Arrays ;
1+ import java .util .Arrays ;
2+ import java .util .Random ;
23public class Main {
4+ public static float affectation = 0 ;
5+ public static float echanges = 0 ;
6+ public static float comparaison = 0 ;
37 public static void main (String [] args ) {
48 System .out .print ("\033 \033 " );
59
6- int [] tableau = { 1 , 3 , 7 , 2 , 6 , 4 , 5 , 8 , 10 , 9 };
10+ int [] tableau = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };
711
812 System .out .println ("Tableau de test : " + Arrays .toString (tri (tableau )));
13+ System .out .println ("Nombre d'échanges : " + echanges );
14+ System .out .println ("Nombre de comparaisons :" + comparaison );
15+ System .out .println ("Nombre d'affectations :" + affectation );
16+ stat (10 ,20 ,5 ,10 );
917
1018 }
1119
1220 public static int [] tri (int [] tableau )
1321 {
14- int taille = tableau .length ;
22+ int taille = tableau .length ;
23+
1524
1625 for (int i = 1 ; i < taille ; i ++)
1726 {
1827 int x = tableau [i ];
19- int j = i -1 ;
28+ int j = i -1 ;
29+
30+ affectation += 2 ;
31+ comparaison ++;
32+
2033
2134 while (j >= 0 && tableau [j ] > x )
2235 {
23- tableau [j +1 ] = tableau [j ];
36+ tableau [j +1 ] = tableau [j ];
37+ echanges ++;
38+ affectation +=1 ;
39+ comparaison +=2 ;
2440 j --;
25- }
26- tableau [j +1 ] = x ;
41+
42+
43+ }
44+ tableau [j +1 ] = x ;
45+
2746 }
28- return tableau ;
47+ return tableau ;
2948 }
30-
49+
50+ public static void stat (int min ,int max , int step , int nbr ) {
51+ int i ;
3152
32- }
53+ for ( i = 1 ; i <= nbr ; i ++) {
54+ int tableau [] =new Random ().ints (min ).toArray ();
55+ tri (tableau );
56+ }
57+ if (min < max && i >nbr ) {
58+ System .out .println ("Le nombre d'affectations pour " + min + " est de " + affectation /nbr );
59+ stat ((min +step ),max ,step ,nbr );
60+ } else {
61+ System .out .println ("Le nombre d'affectations pour " + min + " est de " + affectation /nbr );
62+ }
63+
64+
65+ }
66+ }
0 commit comments