Skip to content

Commit a9cb543

Browse files
committed
V2 final itératif
1 parent e9855f6 commit a9cb543

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import java.util.Arrays;
2+
import java.util.Random;
3+
4+
public class Main {
5+
public static float echanges = 0;
6+
public static float comparaisons = 0;
7+
public static float affectations = 0;
8+
9+
public static void main(String[] args) {
10+
// write your code here
11+
// Clear console
12+
System.out.print("\033[H\033[2J");
13+
14+
// Tableau de test
15+
// int[] tab = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
16+
17+
// System.out.println("Tableau de test : " + Arrays.toString(tri_bulleN(tab)));
18+
19+
stat(10,20,5,10);
20+
21+
22+
}
23+
24+
public static int[] tri_bulleN(int[] tab) {
25+
echanges = 0;
26+
comparaisons = 0;
27+
affectations = 0;
28+
int taille = tab.length;
29+
int tmp = 0;
30+
for (int i = 0; i < taille; i++) {
31+
32+
for (int j = 1; j < (taille - i); j++) {
33+
if (tab[j - 1] > tab[j]) {
34+
tmp = tab[j - 1];
35+
tab[j - 1] = tab[j];
36+
tab[j] = tmp;
37+
38+
echanges++;
39+
comparaisons++;
40+
affectations += 3;
41+
}
42+
comparaisons++;
43+
}
44+
comparaisons++;
45+
}
46+
47+
48+
//System.out.println("Il y a eu " + comparaisons + " comparaisons, " + echanges + " échange et " + affectations + " affectations");
49+
return tab;
50+
}
51+
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);
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+
}
67+
}
68+
}
202 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)