/*-----------------------------------------------------------/ Le code ci-dessous est l’implémentation d’un Tri Shaker. Bref rappel: Ce tri effectue le parcours du tableau de façon alternée Pour une bonne lisibilité, nous avons écrit trois fonctions à savoir la Fonction d’Echange, le Tri proprement dit et pour finir une fonction d’affichage des éléments. /------------------------------------------------------------*/ #include using namespace std; void Ecgange(int *, int , int ); void Tri_Shaker(int *, int , int ); void Afficher(int *, int, int ); //Fonction principale int main() { int Tab[] = {10,6,30,40,7}; cout << "\nValeur du tableau nom trie : \n"; Afficher(Tab, 0, 5); Tri_Shaker(Tab, 0, 5); cout << "\nValeur du tableau trie : \n"; Afficher(Tab, 0, 5); 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 du tri Shaker void Tri_Shaker(int *Tab, int H, int B) { int i, pHaut = H, pBas = B, DerEch = B ; do{ for(i=pBas; i>= pHaut; i--) { if(Tab[i] < Tab[i-1]) { Echange(Tab, i, i-1); DerEch = i; } } pHaut = DerEch + 1; for(i=pHaut; i <= pBas; i++) { if(Tab[i] < Tab[i-1]) { Echange(Tab, i, i-1); DerEch = i; } } pBas = DerEch - 1; }while (pBas >= pHaut); //while (pHaut > pBas); } //Fonction d'affichage des elements du tableau void Afficher(int *Tab, int H, int B) { int i; for(i= H; i< B; i++) cout << Tab[i] <<" | "; cout<< endl; }