#include #include #include #include #include "inf101.h" #include "TrisTableau.h" // Les fonctions sont decrites dans TrisTableau.h void saisir(int t[], int taille) { for (int i = 0; i < taille; ++i) { printf("Entrez la valeur %d du tableau\n", i); t[i] = lire_entier(); } } /* saisir */ void afficher(int t[], int n) { for (int i = 0; i < n; i++) printf("Case %d du tableau = %d\n", i, t[i]); } /* afficher */ bool rechercher(int t[], int taille, int valeur) { for (int i = 0; i < taille; ++i) if (t[i] == valeur) return true; return false; } /* rechercher */ int position_de_valeur_max(int t[], int fin) { assert(fin >= 1); int position = 0; for (int i=1; i < fin; i++) if (t[i] > t[position]) position = i; return position; } /* position_de_valeur_max */ void mettre_max_a_la_fin(int t[], int fin) { int posmax = position_de_valeur_max(t, fin); int z = t[posmax]; t[posmax] = t[fin-1]; t[fin-1] = z; } /* mettre_max_a_la_fin */ void tri_selection(int t[], int taille) { for (int i = taille; i >= 2 ; i--) mettre_max_a_la_fin(t, i); } /* tri_selection */ void echange(int t[], int pos1, int pos2) { int tmp = t[pos1]; t[pos1] = t[pos2]; t[pos2] = tmp; } /* echange */ void tri_bulle(int t[], int taille) { bool fini = false; while(!fini) { fini = true; for (int i = 0; i < taille-1; ++i) if (t[i] > t[i+1]) { fini = false; echange(t, i, i+1); } } } /* tri_bulle */ // Test des fonctions precedentes int main(void) { int x; printf("Test du tri selection\n"); printf("Entrez le nombre d'elements\n"); x = lire_entier(); if (x < 0) { fprintf(stderr, "Le nombre d'elements doit etre > 0\n"); return EXIT_FAILURE; } // Maintenant que le nombre d'elements est connu, // on definit un tableau de cette taille. int tab[x]; saisir(tab, x); printf("Tableau saisi\n"); afficher(tab, x); tri_selection(tab, x); printf("Tableau trie par tri selection\n"); afficher(tab, x); printf("----------------------------\n"); printf("Test du tri bulle\n"); printf("Entrez le nombre d'elements\n"); x = lire_entier(); if (x < 0) { fprintf(stderr, "Le nombre d'elements doit etre > 0\n"); return EXIT_FAILURE; } // Maintenant que le nombre d'elements est connu, // on definit un tableau de cette taille. int tab2[x]; saisir(tab2, x); printf("Tableau saisi\n"); afficher(tab2, x); tri_bulle(tab2, x); printf("Tableau trie par tri bulle\n"); afficher(tab2, x); }