Skip to content

Commit

Permalink
Initial commit. Premiers tris
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Bouvier committed Dec 24, 2011
0 parents commit 2a1408a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tris/insertion/main.cpp
@@ -0,0 +1,32 @@
# include <iostream>
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;
}
42 changes: 42 additions & 0 deletions tris/selection/main.cpp
@@ -0,0 +1,42 @@
#include <iostream>
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;
}

0 comments on commit 2a1408a

Please sign in to comment.