Skip to content
Snippets Groups Projects
Commit d6634e63 authored by LEPPERT MICHAEL's avatar LEPPERT MICHAEL
Browse files

Merge branch 'master' into 'master'

ajout d'un compteur pour les comparaisons, ecritures...

See merge request leppertgrimmer/P4z!9
parents 6ed875b9 e317cc47
1 merge request!1makefile et autres
This commit is part of merge request !1. Comments created here will be created in the context of that merge request.
...@@ -16,14 +16,6 @@ void afficheTab(long tabAAffiche[], size_t tabTaille){ ...@@ -16,14 +16,6 @@ void afficheTab(long tabAAffiche[], size_t tabTaille){
printf(" ]\n"); printf(" ]\n");
} }
void comparer(){
}
void ecrire(){
}
void genTabAlea(long tabARemplir[], size_t tailleTab) { void genTabAlea(long tabARemplir[], size_t tailleTab) {
int rand_is_seeded = 0; int rand_is_seeded = 0;
if(!rand_is_seeded) if(!rand_is_seeded)
...@@ -35,3 +27,14 @@ void genTabAlea(long tabARemplir[], size_t tailleTab) { ...@@ -35,3 +27,14 @@ void genTabAlea(long tabARemplir[], size_t tailleTab) {
tabARemplir[i]=rand()%(10); tabARemplir[i]=rand()%(10);
} }
} }
void compter(int* i){
*i += 1;
}
void affiche_infos(struct compteur cpt){
printf("nombre de comparaisons : %d\n", cpt.comparaison);
printf("nombre d'écritures : %d\n", cpt.ecriture);
printf("nombre de fusions : %d\n", cpt.fusion);
printf("nombre de partitions : %d\n", cpt.partition);
}
...@@ -16,3 +16,6 @@ void genTabAlea(long tabARemplir[], size_t tailleTab); ...@@ -16,3 +16,6 @@ void genTabAlea(long tabARemplir[], size_t tailleTab);
//compte le nombre de comparaisons //compte le nombre de comparaisons
void compter(int* i); void compter(int* i);
//affiche les infos de compteur
void affiche_infos(struct compteur cpt);
...@@ -9,6 +9,12 @@ int main(int argc, char const *argv[]) { ...@@ -9,6 +9,12 @@ int main(int argc, char const *argv[]) {
long tableauBis[20]; long tableauBis[20];
memset(tableauBis, 0, 20*sizeof(long)); memset(tableauBis, 0, 20*sizeof(long));
/*long tab[5] = {3,2,5,1,4};
afficheTab(tab,5);
triRapide(tab,5);
afficheTab(tab,5);*/
printf("--- Tableau initial insertion ---\n"); printf("--- Tableau initial insertion ---\n");
genTabAlea(tableauBis,20); genTabAlea(tableauBis,20);
afficheTab(tableauBis,20); afficheTab(tableauBis,20);
...@@ -17,19 +23,19 @@ int main(int argc, char const *argv[]) { ...@@ -17,19 +23,19 @@ int main(int argc, char const *argv[]) {
printf("\n\n"); printf("\n\n");
/*printf("--- Tableau initial fusion ---\n"); printf("--- Tableau initial fusion ---\n");
genTabAlea(tableauBis,20); genTabAlea(tableauBis,20);
afficheTab(tableauBis,20); afficheTab(tableauBis,20);
triFusion(tableauBis,20); triFusion(tableauBis,20);
afficheTab(tableauBis,20);*/ afficheTab(tableauBis,20);
printf("\n\n"); printf("\n\n");
/*printf("--- Tableau initial rapide ---\n"); printf("--- Tableau initial rapide ---\n");
genTabAlea(tableauBis,20); genTabAlea(tableauBis,20);
afficheTab(tableauBis,20); afficheTab(tableauBis,20);
triRapide(tableauBis,20); triRapide(tableauBis,20);
afficheTab(tableauBis,20);*/ afficheTab(tableauBis,20);
return 0; return 0;
} }
#include "tri_fusion.h" #include "tri_fusion.h"
#include "function.h" #include "function.h"
struct compteur cpt;
void sousTriFusion(long tab[], size_t p, size_t r){ void sousTriFusion(long tab[], size_t p, size_t r){
if(p < r - 1){ if(p < r - 1){
size_t q = (p + r)/2; size_t q = (p + r)/2;
sousTriFusion(tab, p, q); sousTriFusion(tab, p, q);
sousTriFusion(tab, q, r); sousTriFusion(tab, q, r);
fusion(tab, p, q, r); fusion(tab, p, q, r);
compter(&cpt.fusion);
} }
} }
...@@ -16,17 +19,19 @@ void fusion(long tab[], size_t p, size_t q, size_t r){ ...@@ -16,17 +19,19 @@ void fusion(long tab[], size_t p, size_t q, size_t r){
long tabG[n1]; long tabG[n1];
long tabD[n2]; long tabD[n2];
memset(tabD, 0, n1*sizeof(long)); //memset(tabD, 0, n1*sizeof(long));
memset(tabG, 0, n2*sizeof(long)); //memset(tabG, 0, n2*sizeof(long));
size_t j = 0; size_t j = 0;
for(size_t i = p; i < q; i++) { for(size_t i = p; i < q; i++) {
tabG[j] = tab[i]; tabG[j] = tab[i];
j++; j++;
compter(&cpt.ecriture);
} }
j = 0; j = 0;
for(size_t i = q; i < r; i++){ for(size_t i = q; i < r; i++){
tabD[j] = tab[i]; tabD[j] = tab[i];
j++; j++;
compter(&cpt.ecriture);
} }
size_t indG = 0; size_t indG = 0;
...@@ -35,14 +40,17 @@ void fusion(long tab[], size_t p, size_t q, size_t r){ ...@@ -35,14 +40,17 @@ void fusion(long tab[], size_t p, size_t q, size_t r){
while(i < r){ while(i < r){
if(indG == n1){ if(indG == n1){
compter(&cpt.comparaison);
tab[i] = tabD[indD]; tab[i] = tabD[indD];
indD++; indD++;
} }
else if(indD == n2){ else if(indD == n2){
compter(&cpt.comparaison);
tab[i] = tabG[indG]; tab[i] = tabG[indG];
indG++; indG++;
} }
else if(tabG[indG] < tabD[indD]){ else if(tabG[indG] < tabD[indD]){
compter(&cpt.comparaison);
tab[i] = tabG[indG]; tab[i] = tabG[indG];
indG++; indG++;
} }
...@@ -51,10 +59,12 @@ void fusion(long tab[], size_t p, size_t q, size_t r){ ...@@ -51,10 +59,12 @@ void fusion(long tab[], size_t p, size_t q, size_t r){
indD++; indD++;
} }
i++; i++;
compter(&cpt.ecriture);
} }
} }
void triFusion(long tab[], size_t taille){ void triFusion(long tab[], size_t taille){
printf("--- Lancement d'un tri fusion ! ---\n"); printf("--- Lancement d'un tri fusion ! ---\n");
sousTriFusion(tab, 0, taille); sousTriFusion(tab, 0, taille);
affiche_infos(cpt);
} }
#include "tri_insertion.h" #include "tri_insertion.h"
#include "function.h" #include "function.h"
struct compteur cpt;
void tri_insertion(long tabInit[], size_t tailleTab) { void tri_insertion(long tabInit[], size_t tailleTab) {
printf("--- Lancement d'un tri insertion ! ---\n"); printf("--- Lancement d'un tri insertion ! ---\n");
for(size_t i=1; i<tailleTab; i++) { for(size_t i=1; i<tailleTab; i++) {
long clef = tabInit[i]; long clef = tabInit[i];
int j=i-1; int j=i-1;
while(j >= 0 && tabInit[j]>clef) { while(j >= 0 && tabInit[j]>clef) {
compter(&cpt.comparaison);
tabInit[j+1]=tabInit[j]; tabInit[j+1]=tabInit[j];
compter(&cpt.ecriture);
j=j-1; j=j-1;
} }
tabInit[j+1]=clef; tabInit[j+1]=clef;
compter(&cpt.ecriture);
} }
affiche_infos(cpt);
} }
#include "tri_rapide.h" #include "tri_rapide.h"
#include "function.h" #include "function.h"
struct compteur cpt;
void sousTriRapide(long tab[], size_t p, size_t r){ void sousTriRapide(long tab[], size_t p, size_t r){
size_t max = 0; max--; size_t max = 0; max--;
if(r-1 != max){ if(r-1 != max){
if(p < r-1 ){ if(p < r-1 ){
size_t q = partition(tab, p, r); size_t q = partition(tab, p, r);
compter(&cpt.partition);
sousTriRapide(tab, p, q); sousTriRapide(tab, p, q);
sousTriRapide(tab, q+1, r); sousTriRapide(tab, q+1, r);
} }
...@@ -18,15 +21,20 @@ int partition(long tab[], size_t p, size_t r){ ...@@ -18,15 +21,20 @@ int partition(long tab[], size_t p, size_t r){
for(size_t j = p; j <= (r - 2); j++ ){ for(size_t j = p; j <= (r - 2); j++ ){
if(tab[j] <= pivot){ if(tab[j] <= pivot){
compter(&cpt.comparaison);
long temp = tab[j]; long temp = tab[j];
tab[j] = tab[i]; tab[j] = tab[i];
compter(&cpt.ecriture);
tab[i] = temp; tab[i] = temp;
compter(&cpt.ecriture);
i++; i++;
} }
} }
long temp = tab[i]; long temp = tab[i];
tab[i] = tab[r-1]; tab[i] = tab[r-1];
compter(&cpt.ecriture);
tab[r-1] = temp; tab[r-1] = temp;
compter(&cpt.ecriture);
return i; return i;
} }
...@@ -34,4 +42,5 @@ int partition(long tab[], size_t p, size_t r){ ...@@ -34,4 +42,5 @@ int partition(long tab[], size_t p, size_t r){
void triRapide(long tab[], size_t taille){ void triRapide(long tab[], size_t taille){
printf("--- Lancement d'un tri rapide ! ---\n"); printf("--- Lancement d'un tri rapide ! ---\n");
sousTriRapide(tab, 0, taille); sousTriRapide(tab, 0, taille);
affiche_infos(cpt);
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment