|
| 1 | +import java.util.Arrays; |
| 2 | +import java.util.Random; |
| 3 | +public class Iteratif { |
| 4 | + public static float echanges = 0; |
| 5 | + public static float comparaisons = 0; |
| 6 | + public static float affectations = 3*echanges; |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + public static int[] selectedSort(int[] tableau) { |
| 11 | + int i; |
| 12 | + int min; |
| 13 | + int v; |
| 14 | + int pos; |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + for (i = 0; i < tableau.length - 1; i++) { |
| 19 | + pos = i; |
| 20 | + |
| 21 | + affectations+=1; |
| 22 | + comparaisons+=1; |
| 23 | + for (v = i + 1; v < tableau.length; v++) { |
| 24 | + if (tableau[v] < tableau[pos]) { |
| 25 | + pos = v; |
| 26 | + |
| 27 | + comparaisons+=1; |
| 28 | + affectations+=1; |
| 29 | + } |
| 30 | + comparaisons+=1; |
| 31 | + } |
| 32 | + min = tableau[pos]; |
| 33 | + tableau[pos] = tableau[i]; |
| 34 | + tableau[i] = min; |
| 35 | + echanges += 1; |
| 36 | + affectations += 3; |
| 37 | + } |
| 38 | + return tableau; |
| 39 | + } |
| 40 | + public static void main(String[] args) { |
| 41 | + // write your code here |
| 42 | + // Clear console |
| 43 | + System.out.print("\033[H\033[2J"); |
| 44 | + |
| 45 | + // Tableau de test |
| 46 | + int[] tableau = {1,2,3,4,5,6,7,8,9,10}; |
| 47 | + // int[] tableau = {10,9,8,7,6,5,4,3,2,1}; |
| 48 | + // int[] tableau = {1,2,3,4,5,6,7,8,9,10}; |
| 49 | + |
| 50 | + System.out.println("Tableau de test : " + Arrays.toString(selectedSort(tableau))); |
| 51 | + System.out.println("Comparaisons : " + comparaisons); |
| 52 | + System.out.println("Echanges : " + echanges); |
| 53 | + System.out.println("Affectations : " + affectations ); |
| 54 | + |
| 55 | + stat(10,20,5,10); |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + public static void stat(int min,int max, int step, int nbr) { |
| 62 | + int i; |
| 63 | + comparaisons = 0; |
| 64 | + echanges = 0; |
| 65 | + affectations = 0; |
| 66 | + for ( i = 1 ; i <= nbr ; i++) { |
| 67 | + int Tab[] =new Random().ints(min).toArray(); |
| 68 | + selectedSort(Tab); |
| 69 | + } |
| 70 | + if (min < max && i>nbr) { |
| 71 | + System.out.println("Le nombre d'affectations pour " + min + " est de " + affectations/nbr); |
| 72 | + stat((min+step),max,step,nbr); |
| 73 | + } else { |
| 74 | + System.out.println("Le nombre d'affectations pour " + min + " est de " + affectations/nbr); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments