/*-----------------------------------------------------------/ Le code ci-dessous est l’implémentation d’un Tri par extraction ou Tri par sélection. Pour une bonne lisibilité, nous avons écrit quatre fonctions à savoir la Fonction d’Echange, le Tri proprement dit, la fonction qui place le minimum de tableau à Gauche et pourfinir une fonction d’affichage des éléments. /------------------------------------------------------------*/ #include using namespace std; //Prototype de Fonctions /*****************************************************/ void Ecgange(int *, int , int ); void Tri_Extration_iteratif(int *, int , int ); void Place_MinG(int *, int , int); void Afficher(int *, int, int ); /*****************************************************/ //Fonction principale int main() { int Tab[] = {10, 2, 6, 30, 40, 7}; cout << "\nValeur du tableau nom trie : \n"; Afficher(Tab, 0, 6); Tri_Extration_iteratif(Tab, 0, 6); cout << "\nValeur du tableau trie : \n"; Afficher(Tab, 0, 6); return 0; } //Fonction Echage void Echange(int *Tab, int i, int j) { int temp; //variable temporaire pour faciliter la permutation temp = Tab[i]; Tab[i] = Tab[j]; Tab[j] = temp; } //Fonction qui Place le plus peti des valeur à gauche void Place_MinG(int *Tab, int Gauche, int Droite) { int Min = Tab[Gauche], i; int IndiceMin = Gauche; for(i= Gauche +1; i<= Droite; i++) { if(Tab[i] < Min) { Min = Tab[i]; IndiceMin = i; } } //Appelle de la fonction Echange Echange(Tab, Gauche, IndiceMin); } //Fonction du tri par extraction void Tri_Extration_iteratif(int *Tab, int Gauche, int Droite) { int i; for(i=Gauche; i<= Droite - 1; i++) { /*Ici il sufit d'appeller la fonction qui place le Min à gauche et le tour est joué*/ Place_MinG(Tab, i , Droite); } } //Fonction d'affichage des elements du tableau void Afficher(int *Tab, int Gauche, int Droite) { int i; for(i= Gauche; i< Droite; i++) cout << Tab[i] <<" | "; cout<< endl; }