|
| 1 | +import java.util.Arrays; |
| 2 | +import java.util.Random; |
| 3 | + |
| 4 | +public class Iteratif { |
| 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 = { 4, 3, 2, 1}; |
| 16 | + |
| 17 | + // System.out.println("Tableau de test : " + Arrays.toString(tri_bulleN(tab))); |
| 18 | + |
| 19 | + stat(10, 20, 5, 1); |
| 20 | + |
| 21 | + } |
| 22 | + |
| 23 | + public static int[] tri_bulleN(int[] tab) { |
| 24 | + int taille = tab.length; |
| 25 | + int tmp = 0; |
| 26 | + for (int i = 0; i < taille; i++) { |
| 27 | + |
| 28 | + for (int j = 1; j < (taille - i); j++) { |
| 29 | + if (tab[j - 1] > tab[j]) { |
| 30 | + tmp = tab[j - 1]; |
| 31 | + tab[j - 1] = tab[j]; |
| 32 | + tab[j] = tmp; |
| 33 | + |
| 34 | + echanges++; |
| 35 | + comparaisons++; |
| 36 | + affectations += 3; |
| 37 | + } |
| 38 | + comparaisons++; |
| 39 | + affectations++; |
| 40 | + } |
| 41 | + |
| 42 | + affectations++; |
| 43 | + comparaisons++; |
| 44 | + } |
| 45 | + |
| 46 | + return tab; |
| 47 | + } |
| 48 | + |
| 49 | + public static void stat(int min, int max, int step, int nbr) { |
| 50 | + int i; |
| 51 | + comparaisons = 0; |
| 52 | + echanges = 0; |
| 53 | + affectations = 0; |
| 54 | + for (i = 1; i <= nbr; i++) { |
| 55 | + int Tab[] = new Random().ints(min).toArray(); |
| 56 | + // int Tab[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; |
| 57 | + // int Tab[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; |
| 58 | + tri_bulleN(Tab); |
| 59 | + } |
| 60 | + if (min < max && i > nbr) { |
| 61 | + System.out.println("Le nombre d'affectations pour " + min + " est de " + affectations / nbr); |
| 62 | + stat((min + step), max, step, nbr); |
| 63 | + } else { |
| 64 | + System.out.println("Le nombre d'affectations pour " + min + " est de " + affectations / nbr); |
| 65 | + System.out.println("Le nombre de comparaisons est de " + comparaisons / nbr); |
| 66 | + System.out.println("Le nombre d'echanges est de " + echanges / nbr); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments