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
| #include <stdio.h>
#include <stdlib.h>
#include <time.h>
void randomise (int * array, int grandeur)
{
for (int i = 0 ; i < grandeur ; i++)
array[i] = rand() % 51 ;
}
void affiche (int * array, int grandeur)
{
for (int j = 0 ; j < grandeur ; j++)
printf("%4d", array[j]);
printf("\n");
}
int main ()
{
srand(time(NULL)) ;
int * tableau;
int taille ;
printf("quelle est la taille du tableau ? \n");
scanf("%d", &taille) ;
tableau = malloc (taille * sizeof(int)) ;
randomise (tableau, taille) ;
printf("avant tri \n");
affiche (tableau, taille) ;
//je vais parcourir tout le tableau moins une valeur
//à l'intérieur : je compare toutes les valeurs
for (int i = 0 ; i < taille ; i++)
for (int j = 1 ; j < (taille-1) ; j ++)
if (tableau[j] < tableau[j-1])
//permutation
{
//int valeur = tableau[j-1] ;
int temp = tableau[j];
for (j-1 ; tableau[j-1] < tableau[j] ; j--)
tableau[j+1] = tableau[j];
tableau[j] = temp ;
}
printf("apres tri \n");
affiche (tableau, taille) ;
free(tableau) ;
return 0 ;
} |
Partager