1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
| /* le tri strohecker guillaume */
#include <stdio.h>
#include <stdlib.h>
#define NB_MAXI_ELEMENT 10000
/* fonction echange */
void Echange(int *T, int i, int j){
int temp;
temp = T[i];
T[i] = T[j];
T[j] = temp;
}
/* Fonction de partionnement */
int Partionnement(int *T, int imin, int imax){
int pivot, i, j;
pivot = T[imax];
i = imin;
j = imax - 1;
while (i <= j){
while ((i < imax) && (T[i] <= pivot))
i++;
while ((j >= imin) && (T[j] >= pivot))
j--;
if (i < j)
Echange(T, i, j);
}
T[imax] = T[i];
T[i] = pivot;
return i;
}
/* Fonction Tri rapide */
void TriRapide(int *T, int imin, int imax){
int i;
if (imin < imax){
i = Partionnement(T, imin, imax);
TriRapide(T, imin, i-1); /* tri de la partie gauche */
TriRapide(T, i+1, imax); /* tri de la partie droite */
}
}
/* Fonction QuickSort */
void QuickSort(int *T, int taille){
TriRapide(T, 0, taille-1); /* imin = 0 et imax = 1 */
}
/* algorithme de tri par sélection */
static void TriSelection(int *T, int taille){
int k, i, imax;
k = taille - 1;
while (k > 0){
/* recherche de l'indice maximum */
imax = 0;
for (i = 1; i <= k; i++){
if (T[imax] < T[i]){
imax = i;
}
}
/* echange */
Echange(T, k, imax);
k--;
}
}
/* Algorithme de tri à bulle */
static void TriBulle(int tableau[], int taille){
int i, k;
k = taille - 1;
/* pour chaque passe */
while(k > 0){
/* on fait remonter le plus grand */
for (i = 1; i <= k; i++){
if (tableau[i] < tableau[i-1]){
/* échange de T[i-1] et T[i] */
Echange(tableau, i, i-1);
}
}
k--;
}
}
/* Algorithme de tri par insertion */
static void TriInsertion(int tableau[], int taille){
int k, i, v;
for( k = 1; k < taille; k++)
{
v = tableau[k];
i = k - 1;
/* on décale les éléments pour l'insertion */
while(( i>= 0) && ( v <tableau[i]))
{
tableau[i+1] = tableau[i];
i--;
}
/* insertion */
tableau[i+1] = v;
}
}
/* fonction de saisie de tableau */
int SaisieTableau(int tableau[NB_MAXI_ELEMENT],int taille){
size_t i;
static int rand_init = 0;
if (rand_init == 0)
{
srand(time(NULL));
rand_init = 1;
}
if(taille > NB_MAXI_ELEMENT){
puts("Erreur : Le tableau est trop petit");
exit(1);
}
for(i = 0; i < taille; i++){
tableau[i] = rand();
}
return taille;
}
/* fonction d'affichage */
void AffichageTableau(int tableau[], int taille){
int i;
for(i = 0; i < taille; i++)
printf("%d ", tableau[i]);
printf("\n");
}
/* Fonction de choix */
void FaitesVotreChoix(int tableau[], int taille){
char choix ;
int c;
while ((c = fgetc(stdin)) != '\n' && c != EOF);
puts("Faites votre choix :");
puts("Un tri par selection -----> a");
puts("Un tri a bulle -----------> b");
puts("Un tri par insertion -----> c");
puts("Un tri rapide ------------> d");
puts("Quitter ------------------> e");
choix = (char) getchar();
switch(choix){
case'a' :
puts("Avant le tri:");
AffichageTableau(tableau, taille);
TriSelection(tableau, taille);
puts("Apres le tri:");
AffichageTableau(tableau, taille);
break;
case'b' :
puts("Avant le tri:");
AffichageTableau(tableau, taille);
TriBulle(tableau, taille);
puts("Apres le tri:");
AffichageTableau(tableau, taille);
break;
case'c' :
puts("Avant le tri:");
AffichageTableau(tableau, taille);
TriInsertion(tableau, taille);
puts("Apres le tri:");
AffichageTableau(tableau, taille);
break;
case'd' :
puts("Avant le tri:");
AffichageTableau(tableau, taille);
QuickSort(tableau, taille);
puts("Apres le tri:");
AffichageTableau(tableau, taille);
break;
case'e' :
break;
default : puts("Erreur de saisie!!");
}
}
/* programme test */
int main(void){
int tableau[NB_MAXI_ELEMENT];
int taille;
puts("Saisir la taille de votre tableau : ");
scanf("%d", &taille);
SaisieTableau(tableau, taille);
FaitesVotreChoix(tableau, taille);
return 0;
} |
Partager