Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Benjamin Bouvier
committed
Dec 24, 2011
0 parents
commit 2a1408a
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |