diff --git a/tris/insertion/main.cpp b/tris/insertion/main.cpp new file mode 100644 index 0000000..756afdd --- /dev/null +++ b/tris/insertion/main.cpp @@ -0,0 +1,32 @@ +# include +using namespace std; + +const int TAILLE_TAB = 8; + +void triInsertion(int * tab) +// O(n²) +{ + for (int j = 1; j < TAILLE_TAB; ++j) + { + int cle = tab[j]; + int i = j - 1; + while(i >= 0 && tab[i] > cle) + { + tab[i+1] = tab[i]; + --i; + } + tab[i+1] = cle; + } +} + +int main (int argc, char** argv) +{ + int tableau[TAILLE_TAB] = {2, 1, 8, 6, 4, 3, 5, 7}; + triInsertion(tableau); + cout << "Tableau trié : " << endl; + for (int i = 0; i < TAILLE_TAB; ++i) + { + cout << tableau[i] << " "; + } + cout << endl; +} diff --git a/tris/selection/main.cpp b/tris/selection/main.cpp new file mode 100644 index 0000000..eca8686 --- /dev/null +++ b/tris/selection/main.cpp @@ -0,0 +1,42 @@ +#include +using namespace std; +const int TAILLE_TAB = 8; + +void triSelection(int* tab) +// O(n²) +{ + for (int i = 0; i < TAILLE_TAB-1; ++i) + { + // Sélection du minimum + int iMin = i; + int min = tab[i]; + for (int j = i+1; j < TAILLE_TAB; ++j) + { + if (tab[j] < min) + { + min = tab[j]; + iMin = j; + } + } + + // Permutation + if (i != iMin) + { + int temp = tab[i]; + tab[i] = tab[iMin]; + tab[iMin] = temp; + } + } +} + +int main (void) +{ + int tab[TAILLE_TAB] = {2, 4, 3, 6, 8, 7, 1, 5}; + triSelection(tab); + cout << "Tableau trié : " << endl; + for (int i = 0; i < TAILLE_TAB; ++i) + { + cout << tab[i] << " "; + } + cout << endl; +}