You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
155 lines
3.7 KiB
155 lines
3.7 KiB
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
|
|
void echanger(int tab[], int i, int j){
|
|
int tmp;
|
|
tmp = tab[j];
|
|
tab[j] = tab[i];
|
|
tab[i] = tmp;
|
|
}
|
|
|
|
void algo1(int tab[], int nb){
|
|
int echange = 1;
|
|
int i;
|
|
int deb = 0, fin = nb-1;
|
|
while(echange>0){
|
|
echange=0;
|
|
for(i=deb; i < fin; i++){
|
|
if(tab[i]> tab[i+1]){
|
|
echanger(tab, i, i+1);
|
|
echange++;
|
|
}
|
|
}
|
|
fin--;
|
|
for(i=fin; i > deb;i--){
|
|
if(tab[i]< tab[i-1]){
|
|
echanger(tab, i, i-1);
|
|
echange++;
|
|
}
|
|
}
|
|
deb++;
|
|
}
|
|
}
|
|
|
|
void algo2(int tab[], int nb){
|
|
int *cpt;
|
|
int min = tab[0], max = tab[0];
|
|
int i, j;
|
|
for(i=1; i < nb; i++){
|
|
if(min > tab[i]) min = tab[i];
|
|
if(max < tab[i]) max = tab[i];
|
|
}
|
|
cpt = (int*)calloc((max-min+1), sizeof(int));
|
|
if(cpt == NULL){
|
|
printf("pb aloc mémoire\n");
|
|
exit(1);
|
|
}
|
|
for(i = 0; i < nb; i++){
|
|
cpt[tab[i]-min]++;
|
|
}
|
|
j=0;
|
|
for(i = 0; i < max-min+1; i++){
|
|
while(cpt[i] != 0){
|
|
tab[j] = i+min;
|
|
j++;
|
|
cpt[i]--;
|
|
}
|
|
}
|
|
free(cpt);
|
|
}
|
|
|
|
int recherchePos(int tab[], int n, int val){
|
|
int i ;
|
|
for(i = 0 ; i < n ; i++){
|
|
if(val < tab[i]){
|
|
return i ;
|
|
}
|
|
}
|
|
return i ;
|
|
}
|
|
|
|
void algo3(int tab[], int n){
|
|
int i, pos, j, tmp;
|
|
for(i = 1; i < n; i++){
|
|
pos = recherchePos(tab, i, tab[i]);
|
|
if(pos != i){
|
|
tmp = tab[i];
|
|
for(j = i; j > pos; j--){
|
|
tab[j] = tab[j-1];
|
|
}
|
|
tab[pos] = tmp;
|
|
}
|
|
}
|
|
}
|
|
|
|
void test_1() {
|
|
int nb;
|
|
printf("Entrez le nombre d'éléments du tableau : ");
|
|
scanf("%d", &nb);
|
|
|
|
int tab[nb];
|
|
|
|
// Génère un tableau d'éléments avec des nombres aléatoires entre 1 et 100
|
|
srand(time(NULL));
|
|
for (int i = 0; i < nb; i++) {
|
|
tab[i] = rand() % 100 + 1;
|
|
}
|
|
|
|
clock_t debut = clock(); // Début du chronomètre
|
|
algo1(tab, nb); // Appel de la fonction à tester
|
|
clock_t fin = clock(); // Fin du chronomètre
|
|
double temps_execution = (double)(fin - debut) / CLOCKS_PER_SEC; // Calcul du temps d'exécution
|
|
|
|
printf("Temps d'execution : %f secondes\n", temps_execution); // Affichage du temps d'exécution
|
|
}
|
|
|
|
|
|
void test_2() {
|
|
int nb;
|
|
printf("Entrez le nombre d'éléments du tableau : ");
|
|
scanf("%d", &nb);
|
|
|
|
int tab[nb];
|
|
|
|
// Génère un tableau d'éléments avec des nombres aléatoires entre 1 et 100
|
|
srand(time(NULL));
|
|
for (int i = 0; i < nb; i++) {
|
|
tab[i] = rand() % 100 + 1;
|
|
}
|
|
|
|
clock_t debut = clock(); // Début du chronomètre
|
|
algo2(tab, nb); // Appel de la fonction à tester
|
|
clock_t fin = clock(); // Fin du chronomètre
|
|
double temps_execution = (double)(fin - debut) / CLOCKS_PER_SEC; // Calcul du temps d'exécution
|
|
|
|
printf("Temps d'execution : %f secondes\n", temps_execution); // Affichage du temps d'exécution
|
|
}
|
|
|
|
void test_3(){
|
|
int nb;
|
|
printf("Entrez le nombre d'éléments du tableau : ");
|
|
scanf("%d", &nb);
|
|
|
|
int tab[nb];
|
|
|
|
// Génère un tableau d'éléments avec des nombres aléatoires entre 1 et 100
|
|
srand(time(NULL));
|
|
for (int i = 0; i < nb; i++) {
|
|
tab[i] = rand() % 100 + 1;
|
|
}
|
|
|
|
clock_t debut = clock(); // Début du chronomètre
|
|
algo3(tab, nb); // Appel de la fonction à tester
|
|
clock_t fin = clock(); // Fin du chronomètre
|
|
double temps_execution = (double)(fin - debut) / CLOCKS_PER_SEC; // Calcul du temps d'exécution
|
|
|
|
printf("Temps d'execution : %f secondes\n", temps_execution); // Affichage du temps d'exécution
|
|
}
|
|
|
|
int main(){
|
|
//test_1();
|
|
//test_2();
|
|
//test_3();
|
|
return 0;
|
|
} |