| 12
 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
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 
 |  
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define TRUE 1
#define FALSE 0
 
// tri par insertion
void tri_insertion(int *t, int n)
{
int i,p,j;
int x;
for (i = 1; i < n; i++)
{
x = t[i];
p = i-1;
while (t[p] > x && p-- > 0) {}
p++;
for (j = i-1; j >= p; j--) {
t[j+1] = t[j];
}
t[p] = x;
}
}
 
// tri par fusion
void fusion(int *t,int deb1,int fin1,int fin2)
{
int *table1;
int deb2=fin1+1;
int compt1=deb1;
int compt2=deb2;
int i;
table1=(int *)malloc((fin1-deb1+1)*sizeof(int));
for(i=deb1;i<=fin1;i++)
table1[i-deb1]=t[i];
for(i=deb1;i<=fin2;i++){
if (compt1==deb2) break;
else if (compt2==(fin2+1)) {
t[i]=table1[compt1-deb1];
compt1++;
}
else if (table1[compt1-deb1]<t[compt2]){
t[i]=table1[compt1-deb1];
compt1++;
}
else{
t[i]=t[compt2];
compt2++;
}
}
free(table1);
}
void tri_fusion_bis(int *t,int deb,int fin)
{
if (deb!=fin){
int milieu=(fin+deb)/2;
tri_fusion_bis(t,deb,milieu);
tri_fusion_bis(t,milieu+1,fin);
fusion(t,deb,milieu,fin);
}
}
void tri_fusion(int *t,int n)
{
if (n>0) tri_fusion_bis(t,0,n-1);
}
 
 
 
// tri rapide
 
 
int partitionner(int *tableau, int p, int r) 
{
    int pivot = tableau[p], i = p-1, j = r+1;
    int temp;
    while (1) {
        do
            j--;
        while (tableau[j] > pivot);
        do
            i++;
        while (tableau[i] < pivot);
        if (i < j) {
            temp = tableau[i];
            tableau[i] = tableau[j];
            tableau[j] = temp;
        }
        else
            return j;
    }
}
 
void tri_rapide(int *tableau, int p, int r)
 {
    int q;
    if (p < r) {
        q = partitionner(tableau, p, r);
        tri_rapide(tableau, p, q);
        tri_rapide(tableau, q+1, r);
    }
}
//remplisage du tableau
void remplir_tab(int *t,int n)
{int i;
  printf("\n");
for(i=0;i<n;i++){
printf("Donner l'entier %d: ",i+1);
scanf("%d",&t[i]);
}   
} 
//pire cas
void remplir_tableau_decroissant(int * t,int n)
{int i;
i=0;            
 while(i<n)                                   //remplir le tableau pour la 1ere fois.
{
t[i]=n-i;
 i++;     
}
printf("\n  le tableau est bien rempli par ordre decroissant\n");
 
}
 
//meilleur cas
void remplir_tableau_croissant(int * t,int n)
{int i;
i=0;            
while(i<n)   //remplir le tableau pour la 1ere fois.
{
 t[i]=i;
 i++;     
}
printf("\n  le tableau est bien rempli par ordre decroissant\n");
}
 
//tri par tas
 
 
void initTableau(int *tab, int taille){
	int i;
	for(i = 0; i < taille; i++)
		tab[i] = rand();
}
 
void swap(int *a, int *b){
	int x = *a;
	*a = *b;
	*b = x;
}
 
char domine( int *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 */
}
 
void retablirTas( int *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) */
			}
		}
	}
}
 
void faireTas(int *tab, int taille){
	int k;
	for(k = taille-1; k >= 0; k--)
		retablirTas(tab, k, taille);
}
 
int main(int argc, char *argv[])
{
 printf("*******************************************************************************\n"); 
 printf(" Universit%c de la science et de la technologie HOUARI BOUMEDIENNE 2012/2013     \n",130);
 printf("                       Module : complexit%c  MASTER: IL                          \n",130);
 printf("                                Mini-projet N 2                                  \n",130);
 printf("     Etude exp%crimentale des algorithmes de recherche et des algorithmes de tri \n",130);
 printf("     R%calis%c par : TIHAR KHADIDJA  *  SILHADI ISMAHANE   *   SENOUCI Reda           \n",130,130);
 printf("    Travail r%calis%c sur une machine de processus : i5 CPU @ 2.4GHZ          \n",130,130);
 printf("********************************************************************************\n");
 
    int n=0,i,num,f,j,x;
    int *t;
    clock_t Debut,Fin;
    float Temp;
  int c;
 
 
  printf("donner la taille de l'ensemble des donn%ces N= ",130);
  scanf("%d",&n);
  f=n;
  t=(int *)malloc((n+1)*sizeof(int));
  printf("\n Le tableau est rempli avec %d valeurs al%catoire...\n",n,130,130,130,130);
 
  for(i=0;i<n;i++)
//n valeurs généré aléatoirement
  t[i]=rand();
 
   // liste des algorithmes de tri
   while(1)
    {
printf("\n-------------------------------------Menu--------------------------------------\n");
printf("\n1. Le tri par insertion\n",133);
printf("2. Le tri par fusion\n",130,130);
printf("3. Le tri rapide\n");
printf("4. Le tri par tas\n");
printf("5. Afficher les %cl%cments du tableau\n",130,130);
printf("6. Modifier le tableau et le remplir manuellement\n",130,130);
printf("7. Modifier le tableau et le remplir par ordre decroissant (pire des cas)\n");
printf("8. Modifier le tableau et le remplir par ordre croissant (meilleur des cas)\n");
 
printf("9. Quitter\n");
 
// choisir l'algorithme à appliquer
do{
printf("\nVeuillez saisir votre choix dans le menu: ");
scanf("%d",&num);
if((num>9)||(num<1)) printf("\n(!) Ce num%cro ne figure pas dans la liste !\n",130);}
while((num>9)||(num<1));
// appliquer l'algorithme choisi
 
if(num==1)
{
Debut=clock();
tri_insertion(t,n);
Fin=clock();
Temp=(float)(Fin-Debut)/CLOCKS_PER_SEC;
printf("\n\nLe temps d'execution est : %f \n",Temp);
printf("\nAppuyer sur n'importe quelle touche pour continuer\n");
getch();
 
}
 
if(num==2)
{Debut=clock();
 tri_fusion(t,n);
Fin=clock();
Temp=(float)(Fin-Debut)/CLOCKS_PER_SEC;
printf("\n\nLe temps d'execution est : %f ",Temp);
printf("\nAppuyer sur n'importe quelle touche pour continuer\n");
getch();
}
 
 
if(num==3) 
{
Debut=clock();
tri_rapide(t, 0, n-1);
Fin=clock();
Temp=(float)(Fin-Debut)/CLOCKS_PER_SEC;
printf("\n\nLe temps d'execution est : %f ",Temp);
printf("\nAppuyer sur n'importe quelle touche pour continuer\n");
getch();
}
 
 
if(num==4)
{
initTableau(t,n);
Debut=clock();
faireTas(t, n);
while ( f > 1){ 
swap(&t[0], &t[f-1]);
retablirTas(t, 0, f-1); 
f--;
        }
Fin=clock();
Temp=(float)(Fin-Debut)/CLOCKS_PER_SEC;
printf("\n\nLe temps d'execution est : %f ",Temp);
printf("\nAppuyer sur n'importe quelle touche pour continuer\n");
getch();
}
 
if(num==5)
{
 printf("Les %cl%cments du tableau sont :",130,130); 
      for(i=0;i<n;i++)
    printf(" %d ",t[i]); 
    printf("\nAppuyer sur n'importe quelle touche pour continuer\n");
getch(); 
 
 }
 if(num==6)
 {
           printf("\nL'ancienne taille de tableau est de : %d\n",n);
            printf("donnez la nouvelle taille du tableau : \n");
            scanf("%d",&n);            //nombre d'elements du tableau.
            t=(int*)malloc((n+1)*sizeof(int));
            remplir_tab(t,n);
            printf("\nAppuyer sur n'importe quelle touche pour continuer\n");
getch();
 
 }
 
 if(num==7)
 {
           printf("\nL'ancienne taille du tableau est de : %d\n",n);
            printf("donnez la nouvelle taille du tableau : \n");
            scanf("%d",&n);            //nombre d'elements du tableau.
            t=(int*)malloc((n+1)*sizeof(int));
            remplir_tableau_decroissant(t,n);
            printf("\nAppuyer sur n'importe quelle touche pour continuer\n");
getch();
   }
   if(num==8)
   {
             printf("\nL'ancienne taille de tableau est de : %d\n",n);
            printf("donnez la nouvelle taille du tableau : \n");
            scanf("%d",&n);            //nombre d'elements du tableau.
            t=(int*)malloc((n+1)*sizeof(int));
            remplir_tableau_croissant(t,n); 
            printf("\n\nAppuyer sur n'importe quelle touche pour continuer\n");
getch(); 
             }
 
 
 
if(num==9)
{
          exit(0);
}
//affichage du temps d'execution
 
 
printf("\n \n");
}
  system("PAUSE");	
  return 0;
} | 
Partager