Tri bulles randomise : choix au hasard de deux indice du tableau

Fermé
Aboubacar - 7 mai 2020 à 20:22
yg_be Messages postés 22698 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 18 avril 2024 - 8 mai 2020 à 15:21
bonsoir tout le monde, j'ai implemente un programme de tri bulles randomise qui mais le code ne tri pas mon tableau, quelqu'un peut m'aider a ameliorer mon code pour atteindre mon objectif ?
merci a toutes et a tous.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
//#include <ctime.h>

int main()
{
/* initialisation d'un tableau aleatoire*/
int tabAl[10];
int i,j;
int temp, x;
int passage=0;
bool permut;
clock_t temps_init, temps_fina;
float temps_cpu;
srand(time(NULL));
for(i=0;i<6;i++)
{
 j=rand()%42;
 tabAl[i]=j+1;
 printf("|%d|-->%d \n", i, tabAl[i]);


}
temps_init = clock();
do{ 
permut = false;
 int x1 = rand()%6;
 int x2 = rand()%6;
printf("x1= %d, x2=%d \n", x1,x2);
if( x1 < x2){
  if(tabAl[x1]>tabAl[x2])
    {
     temp = tabAl[x2];
     tabAl[x2] = tabAl[x1];
     tabAl[x1] = temp;
     permut = true;
 }
}else
 {
    if(tabAl[x2]>tabAl[x1])
    {
     temp = tabAl[x1];
     tabAl[x1] = tabAl[x2];
     tabAl[x] = temp;
     permut = true;
    }
  }
}while(permut==true);
temps_fina = clock();
temps_cpu = (float)( temps_fina - temps_init)/CLOCKS_PER_SEC;
printf("la duree de l'algorithme de tri est %2fs \n", temps_cpu);
/*affichage du tableau trie */
printf("\n\naffichage du tableau trie\n\n");
for(i=0; i<6; i++)
{
 printf("|%d|-->%d \n", i, tabAl[i]);
}
return 0;
}
,



Configuration: Linux / Firefox 61.0
A voir également:

1 réponse

yg_be Messages postés 22698 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 18 avril 2024 1 471
8 mai 2020 à 15:16
bonjour, que fait ton code?
0
yg_be Messages postés 22698 Date d'inscription lundi 9 juin 2008 Statut Contributeur Dernière intervention 18 avril 2024 1 471
8 mai 2020 à 15:21
à quoi sert la variable x?
0