Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <math.h>
- #include <limits.h>
- typedef int *TABLEAU;
- typedef int bool;
- #define TRUE 1
- #define FALSE 0
- #define N 10 //n EXPERIENCES
- #define CODE_TRI_INSERTION 0
- #define CODE_TRI_BULLE 1
- #define CODE_TRI_RAPIDE 2
- #define CODE_TRI_FUSION 3
- #define CODE_TRI_TAS 4
- #define CODE_TRI_BASE 5
- #define CODE_TRI_RAPIDE_RANDOM 6
- #define CODE_TRI_RAPIDE_ORIGINAL 7
- /******* créer aleatoirement un tableau de nb_elts entiers compris entre 0 et val_max-1 *****/
- TABLEAU generer_tableau(int nb_elts, int val_max)
- {
- int i;
- TABLEAU t=NULL;
- t=malloc(nb_elts*sizeof(int));
- for (i=0;i<nb_elts;i++)
- t[i]=(rand()%val_max);
- return t;
- }
- /******** afficher un tableau *********/
- void affiche_tableau(TABLEAU t, int nb_elts){
- int i;
- for (i=0;i<nb_elts;i++)
- printf("t[%d]= %d \n",i,t[i]);
- printf("\n");
- return;
- }
- /* crée une copie d'un tableau */
- TABLEAU recopie(TABLEAU t, int nb_elts)
- {
- TABLEAU tc=NULL;
- tc=malloc(nb_elts*sizeof(int));
- int i ;
- for(i=0; i<nb_elts;i++)
- tc[i]=t[i];
- return tc ;
- }
- /**************************Tri_Insertion***********************/
- /******Insert******/
- void insertion(TABLEAU t, int dernier_indice, int x){
- int i=dernier_indice;
- while (i>=0 && t[i]>x)
- {
- // shift: Elements > x
- t[i+1]=t[i];
- i--;
- }
- // In all cases, i+1 : indecates where to insert x
- t[i+1]=x;
- return ;
- }
- /*****tri par insertion*****/
- void triParInsertion(TABLEAU t, int nb_elts){
- int j;
- for (j=1;j<nb_elts;j++)
- // here we have to insert t[j] in t[0..j-1]
- insertion(t,j-1,t[j]);
- }
- /***************************SWAP version1******************************/
- void permuter(TABLEAU t, int i, int j){
- int temp=t[i];
- t[i]=t[j];
- t[j]=temp;
- return;
- }
- /*********swap version 2********/
- void swap(TABLEAU a, TABLEAU b)
- {
- TABLEAU x = a;
- a = b;
- b = x;
- return;
- }
- /**********************tri_bulle********************************/
- void triBulle(TABLEAU t, int nb_elts)
- {
- int end=FALSE;
- int i;
- while (!end)
- {
- end=TRUE;
- for (i=0;i<nb_elts-1;i++)
- if (t[i]>t[i+1])
- {
- permuter(t,i,i+1);
- end=FALSE;
- }
- nb_elts--;
- }
- }
- /*********************Quick_Sort***************************/
- void trirapide (TABLEAU tableau, int taille) {
- int mur, courant, pivot, tmp;
- if (taille < 2) return;
- // On prend comme pivot l element le plus a droite
- pivot = tableau[taille - 1];
- mur = courant = 0;
- while (courant<taille) {
- if (tableau[courant] <= pivot) {
- if (mur != courant)
- {
- tmp=tableau[courant];
- tableau[courant]=tableau[mur];
- tableau[mur]=tmp;
- }
- mur ++;
- }
- courant ++;
- }
- trirapide(tableau, mur - 1);
- trirapide(tableau + mur - 1, taille - mur + 1);
- return;
- }
- /***************randomized pivot quicksort************/
- int random_partition(TABLEAU arr, int start, int end)
- {
- srand(time(NULL));
- int pivotIdx = start + rand() % (end-start+1);
- int pivot = arr[pivotIdx];
- permuter(arr,pivotIdx,end); // move pivot element to the end
- pivotIdx = end;
- int i = start -1;
- for(int j=start; j<=end-1; j++)
- {
- if(arr[j] <= pivot)
- {
- i = i+1;
- permuter(arr,i, j);
- }
- }
- permuter(arr,i+1,pivotIdx);
- return i+1;
- }
- void random_quick_sort(TABLEAU arr, int start, int end)
- {
- if(start < end)
- {
- int mid = random_partition(arr, start, end);
- random_quick_sort(arr, start, mid-1);
- random_quick_sort(arr, mid+1, end);
- }
- return;
- }
- /************************quick_sort version original*****************/
- int partition2( TABLEAU a, int l, int r) {
- int pivot, i, j, t;
- pivot = a[l];
- i = l; j = r+1;
- while( 1)
- {
- do ++i; while( a[i] <= pivot && i <= r );
- do --j; while( a[j] > pivot );
- if( i >= j ) break;
- t = a[i]; a[i] = a[j]; a[j] = t;
- }
- t = a[l]; a[l] = a[j]; a[j] = t;
- return j;
- }
- void quick__Sort( TABLEAU a, int l, int r)
- {
- int j;
- if( l < r )
- {
- // divide and conquer
- j = partition2( a, l, r);
- quick__Sort( a, l, j-1);
- quick__Sort( a, j+1, r);
- }
- return;
- }
- /****************************Merge_Sort******************************/
- void fusion(TABLEAU tableau,int deb1,int fin1,int fin2)
- {
- TABLEAU table1=NULL;
- int deb2=fin1+1;
- int compt1=deb1;
- int compt2=deb2;
- int i;
- table1=malloc((fin1-deb1+1)*sizeof(int));
- //We recopies elements from the biginning of the array
- for(i=deb1;i<=fin1;i++)
- {
- table1[i-deb1]=tableau[i];
- }
- for(i=deb1;i<=fin2;i++)
- {
- if (compt1==deb2) //All the elements of the first array have been used
- {
- break; //all the elements have been closed
- }
- else if (compt2==(fin2+1)) //All the elements of the Second array have been used
- {
- tableau[i]=table1[compt1-deb1]; //We add the remaining elements of the first array
- compt1++;
- }
- else if (table1[compt1-deb1]<tableau[compt2])
- {
- tableau[i]=table1[compt1-deb1]; //We add an element of the first array
- compt1++;
- }
- else
- {
- tableau[i]=tableau[compt2]; //We add an element of the second array
- compt2++;
- }
- }
- free(table1);
- return;
- }
- void tri_fusion_bis(TABLEAU tableau,int deb,int fin)
- {
- if (deb!=fin)
- {
- int milieu=(fin+deb)/2;
- tri_fusion_bis(tableau,deb,milieu);
- tri_fusion_bis(tableau,milieu+1,fin);
- fusion(tableau,deb,milieu,fin);
- }
- return;
- }
- void tri_fusion(TABLEAU tableau,int nb_elts)
- {
- if (nb_elts>0)
- {
- tri_fusion_bis(tableau,0,nb_elts-1);
- }
- return;
- }
- /************************HEAP_Sort*************************/
- /************domine****************/
- char domine( TABLEAU tab, int taille, int j )
- {
- if( 2*(j+1)-1 >= taille) /* tab[j] est seul */
- return TRUE;
- else if(2*(j+1)-1 == taille-1 && tab[j] >= tab[2*(j+1)-1]) /* tab[j] a 1 descendant et domine */
- return TRUE;
- else if(tab[j] >= tab[2*(j+1)-1] && tab[j] >= tab[2*(j+1)]) /* tab[j] a 2 descendants et domine */
- return TRUE;
- return FALSE; /* Dans le cas ou tab[j] n'est pas seul et ne domine pas */
- }
- /*********retablirtas********************/
- void retablirTas( TABLEAU tab, int j, int taille )
- {
- while(!domine(tab, taille, j)){/* I(j) et j domine => tab[0...taille-1] est un tas.*/
- if( 2*(j+1) == taille){ /* degre 1 && I(2(j+1)-1) */
- swap(&tab[j], &tab[ 2*(j+1)-1 ]);
- j = 2*(j+1)-1;
- }
- else{ /* degre 2 */
- if( tab[ 2*(j+1)-1 ] >= tab[ 2*(j+1) ] ){ /* I(2(j+1)-1) */
- swap( &tab[ 2*(j+1)-1 ], &tab[j] );
- j = 2*(j+1)-1; /* I(j) */
- }
- else{ /*I(2*(j+1)) */
- swap(&tab[ 2*(j+1) ], &tab[j]);
- j = 2*(j+1); /* I(j) */
- }
- }
- }
- return;
- }
- /*************MAKE_HEAP****************/
- void faireTas(TABLEAU tab, int taille)
- {
- int k;
- for(k = taille-1; k >= 0; k--)
- retablirTas(tab, k, taille);
- return;
- }
- /********TRIPARTAS***************/
- void tripartas(TABLEAU t,int taille)
- {
- int f = taille;
- faireTas(t, taille);
- /* I(f) = tab[0 ... f-1] est un tas
- && ensemble des elements de tab[0 ... f-1] inferieur ou egal
- a ensemble des elements tab[f ... taille-1]
- && tab[f ... taille-1] est trie croissant
- */
- while ( f > 1){ /* I(k) && (k>1)*/
- swap(&t[0], &t[f-1]);
- retablirTas(t, 0, f-1); /* I(k-1) */
- f--;
- }
- return;
- }
- /*********************Radix_sort(tri par base)******/
- int getMax(TABLEAU arr, int nb_elts) {
- int i, max;
- max = arr[0];
- for(i = 1; i < nb_elts; i++)
- {
- if(arr[i] > max) {
- max = arr[i];
- }
- }
- return max;
- }
- void countSort(TABLEAU arr, int n, int exp)
- {
- int output[n]; // output array
- int i, count[10] = {0};
- // Store count of occurrences in count[]
- for (i = 0; i < n; i++)
- count[ (arr[i]/exp)%10 ]++;
- // Change count[i] so that count[i] now contains actual
- // position of this digit in output[]
- for (i = 1; i < 10; i++)
- count[i] += count[i - 1];
- // Build the output array
- for (i = n - 1; i >= 0; i--)
- {
- output[count[ (arr[i]/exp)%10 ] - 1] = arr[i];
- count[ (arr[i]/exp)%10 ]--;
- }
- // Copy the output array to arr[], so that arr[] now
- // contains sorted numbers according to current digit
- for (i = 0; i < n; i++)
- arr[i] = output[i];
- }
- void radixSort(TABLEAU arr, int n) {
- // Find the maximum number to know number of digits
- int m = getMax(arr, n);
- // Do counting sort for every digit. Note that instead
- // of passing digit number, exp is passed. exp is 10^i
- // where i is current digit number
- for (int exp = 1; m/exp > 0; exp *= 10)
- countSort(arr, n, exp);
- }
- /********************fonction de test*********************/
- char test(int nb_elts, int code_tri)
- {
- TABLEAU t=malloc(nb_elts*sizeof(int));
- TABLEAU s=malloc(nb_elts*sizeof(int));
- int i,j,v=0,aux;
- for (i=0;i<nb_elts;i++)
- s[i]=t[i]=(rand()%2?v:v++);
- for (i=0;i<nb_elts;i++)
- {
- j=rand()%nb_elts;
- aux=s[i];s[i]=s[j];s[j]=aux;
- }
- switch (code_tri)
- {
- case CODE_TRI_INSERTION:
- triParInsertion(s, nb_elts);break;
- case CODE_TRI_BULLE:
- triBulle(s, nb_elts);break;
- case CODE_TRI_RAPIDE:
- trirapide(s, nb_elts);break;
- case CODE_TRI_FUSION:
- tri_fusion(s, nb_elts);break;
- case CODE_TRI_TAS:
- tripartas(s, nb_elts);break;
- case CODE_TRI_BASE:
- radixSort(s, nb_elts);break;
- case CODE_TRI_RAPIDE_RANDOM:
- random_quick_sort(s,0, nb_elts-1);break;
- case CODE_TRI_RAPIDE_ORIGINAL:
- quick__Sort(s,0,nb_elts-1);break;
- }
- int res = 1;
- for (i=0; i<nb_elts; i++)
- if (t[i] != s[i])
- {
- res = 0;
- break;
- }
- free(s);
- free(t);
- return res;
- }
- /***********************display**********************/
- void DISPLAY()
- {
- printf("en cours");
- return;
- }
- /***************** affiche_nom_tri(code)****************/
- void affiche_nom_tri(int code)
- {
- switch (code)
- {
- case 0:
- printf("CODE_TRI_INSERTION \n");break;
- case 1:
- printf("CODE_TRI_BULLE \n");break;
- case 2:
- printf("CODE_TRI_RAPIDE\n");break;
- case 3:
- printf("CODE_TRI_FUSION \n");break;
- case 4:
- printf("CODE_TRI_TAS\n");break;
- case 5:
- printf("CODE_TRI_BASE\n");break;
- }
- return;
- }
- void determinertri(TABLEAU s , int code, int nb_elts)
- {
- switch (code)
- {
- case 0:
- triParInsertion(s, nb_elts);break;
- case 1:
- triBulle(s, nb_elts);break;
- case 2:
- trirapide(s, nb_elts);break;
- case 3:
- tri_fusion(s, nb_elts);break;
- case 4:
- tripartas(s, nb_elts);break;
- case 5:
- radixSort(s,nb_elts);break;
- }
- }
- /******************************main2****************************************/
- /*COMPARAISON DES TEMPS D'ÉXECUTION DES ALGORITHMES DE TRI */
- void main2()
- {
- /* tableau contenant les résultats ; les lignes sont indexées par les codes des tris et la colonne i correspond à un tableau de taille (i+1)*base_elts */
- double resultat[6][10]={{0},{0}};
- /* initialisation à 0.0 de chaque case du tableau de résultats */
- int nbExpe=10;
- int base_elts=1000;
- int nb_elts;
- int k , i ;
- TABLEAU t,t2;
- int code;
- int fin,deb;
- double mesure=0;
- srand(time(NULL));
- int valMax=100;
- /***********************/
- for (k=1;k<=10;k++){ // on remplit le tableau par colonne
- nb_elts=k*base_elts;
- printf("%d \n",k);
- /***********************/
- for (i=0;i<nbExpe;i++){ // chaque case du tableau contiendra la somme des temps d'exécution de nbExpe expériences
- /* un même tableau sera trié successivement par tous les algorithmes */
- t=generer_tableau(nb_elts,valMax);
- t2=malloc(k*base_elts*sizeof(int));
- t2=recopie(t,nb_elts);
- for (code=0;code<6;code++){
- printf("calcul en cours ne quittez pas \n ");
- t=recopie(t2,nb_elts);
- deb=clock();
- determinertri(t,code,nb_elts);
- fin=clock();
- mesure=mesure+(double)(fin-deb)/ CLOCKS_PER_SEC;
- resultat[code][i]=mesure;
- }
- free(t);
- free(t2);
- }
- }
- printf("fini");
- /* affichage du tableau de résultat */
- for (code=0;code<6;code++){
- affiche_nom_tri(code);
- for (k=1;k<=10;k++)
- printf("%f ",resultat[code][k-1]/nbExpe);
- printf("\n");
- }/*fin for1*/
- return;
- }/*fin main2*/
- /**********************principal_program***************/
- int main()
- {
- int h, L =10;
- int somme=0;
- int reponse;
- printf("**************************LES TRIS***************************\n");
- printf(" AIX_MARSEILLE UNIVERSITÉ\n ++Menu++ \n ");
- printf("1 : Les details et les differentes parties du programme \n");
- printf("\n 2 : COMPARAISON DES TEMPS D'ÉXECUTION DES ALGORITHMES DE TRI\n ");
- printf("\n 3 : Application des tri \n");
- printf("\n 0 : To EXIT");
- printf("\n************PROGRAMME FAIT PAR :TRABELSI Ilef**************\n");
- printf("*your ANSWER is :");
- scanf("%d",&reponse);
- switch (reponse)
- {
- case 1:
- DISPLAY;break;
- case 0:exit(0);break;
- case 2: main2 ();break;
- case 3:
- {
- int r,debut=0;
- do
- {
- TABLEAU t;
- int fin,deb;
- srand(time(NULL));
- int nb_elts;
- printf("le nb_elts = ");
- scanf("%d",&nb_elts);
- int valMax=100;
- /*******tri_insertion***********/
- printf("The first array: \n");
- t=generer_tableau(nb_elts,valMax);
- affiche_tableau(t,nb_elts);
- printf("tri par Insertion :\n");
- deb=clock();
- triParInsertion(t,nb_elts);
- fin=clock();
- printf("temps d'exécution :%f\n",(double)(fin-deb)/ CLOCKS_PER_SEC);
- affiche_tableau(t,nb_elts);
- free(t);
- /*********tri_Ã _bulle**********/
- printf("the second array \n");
- t=generer_tableau(nb_elts,valMax);
- affiche_tableau(t,nb_elts);
- printf("tri à bulle : \n");
- deb=clock();
- triBulle(t,nb_elts);
- fin=clock();
- printf("temps d'exécution :%f\n",(double)(fin-deb)/ CLOCKS_PER_SEC);
- affiche_tableau(t,nb_elts);
- free(t);
- /*********tri_rapide**********/
- printf("the third array \n");
- t=generer_tableau(nb_elts,valMax);
- affiche_tableau(t,nb_elts);
- printf(" tri Rapide : \n");
- deb=clock();
- trirapide(t,nb_elts);
- fin=clock();
- printf("temps d'exécution :%f\n",(double)(fin-deb)/ CLOCKS_PER_SEC);
- affiche_tableau(t,nb_elts);
- free(t);
- /********tri_fusion*****/
- printf("the fourth array \n");
- t=generer_tableau(nb_elts,valMax);
- affiche_tableau(t,nb_elts);
- printf("tri fusion : \n");
- deb=clock();
- tri_fusion(t,nb_elts);
- fin=clock();
- printf("temps d'exécution :%f\n",(double)(fin-deb)/ CLOCKS_PER_SEC);
- affiche_tableau(t,nb_elts);
- free(t);
- /********tri_par_tas*****/
- printf("the Fithf array \n");
- t=generer_tableau(nb_elts,valMax);
- affiche_tableau(t,nb_elts);
- printf("tri par tas : \n");
- deb=clock();
- tripartas(t,nb_elts);
- fin=clock();
- printf("temps d'exécution :%f\n",(double)(fin-deb)/ CLOCKS_PER_SEC);
- affiche_tableau(t,nb_elts);
- free(t);
- /********tri_par_base*****/
- printf("the Sixth array \n");
- t=generer_tableau(nb_elts,valMax);
- affiche_tableau(t,nb_elts);
- printf("tri par base : \n");
- deb=clock();
- radixSort(t,nb_elts);
- fin=clock();
- printf("temps d'exécution :%f\n",(double)(fin-deb)/ CLOCKS_PER_SEC);
- affiche_tableau(t,nb_elts);
- free(t);
- /********tri_rapide(RANDOM)*****/
- printf("the seventh array \n");
- t=generer_tableau(nb_elts,valMax);
- affiche_tableau(t,nb_elts);
- printf(" random_quick_sort : \n");
- deb=clock();
- random_quick_sort(t,0,nb_elts-1);
- fin=clock();
- printf("temps d'exécution :%f\n",(double)(fin-deb)/ CLOCKS_PER_SEC);
- affiche_tableau(t,nb_elts);
- free(t);
- /********tri_rapide_VERSION_ORIGINAL*****/
- printf("the eighth array \n");
- t=generer_tableau(nb_elts,valMax);
- affiche_tableau(t,nb_elts);
- printf("quick_sort_VERSION_ORIGINALE : \n");
- deb=clock();
- quick__Sort(t,0,nb_elts-1);
- fin=clock();
- printf("temps d'exécution :%f\n",(double)(fin-deb)/ CLOCKS_PER_SEC);
- affiche_tableau(t,nb_elts);
- free(t);
- /**********test**********/
- printf("LETS test our program \n");
- nb_elts=10000;
- if(test(nb_elts,CODE_TRI_INSERTION)==1)
- printf("Test du tri par insertion :GOOD!! \n");
- if(test(nb_elts,CODE_TRI_BULLE)==1)
- printf("Test du tri à bulle :GOOD ! \n");
- if(test(nb_elts,CODE_TRI_RAPIDE)==1)
- printf("Test du tri rapide : GOOD ! \n");
- if(test(nb_elts,CODE_TRI_FUSION)==1)
- printf("Test du tri fusion : GOOD ! \n");
- if(test(nb_elts,CODE_TRI_TAS)==1)
- printf("Test du tri par tas : GOOD ! \n");
- if(test(nb_elts,CODE_TRI_BASE)==1)
- printf("Test du tri par BASE : GOOD ! \n");
- if(test(nb_elts,CODE_TRI_RAPIDE_RANDOM)==1)
- printf("Test du tri rapide(pivot:random) : GOOD ! \n");
- if(test(nb_elts,CODE_TRI_RAPIDE_ORIGINAL)==1)
- printf("Test du tri rapide(ORIGINAL) : GOOD ! \n");
- printf("*********************************************\n");
- printf("* Do you wanna exit ? please press 0:yes 1:No \n*Thank you !*\n");
- printf("*your ANSWER is :");
- scanf("%d",&r);
- }/*fin do */
- while(r==0);
- break;}/*fin case 3*/
- }/*finswitch*/
- return 0;
- }/*fin main*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement