Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void afficheTableau(int tab[], int nbElem){
  4. int b;
  5. printf("[");//il a demandé d'afficher le tableau sous cette forme : [e1, e2, e3, ... ] donc il faut afficher un crochet avant et apres l'affichage du tableau
  6. // il fait egalement apres l'affichage de chaque element afficher une virgule [e1, e2, e3, ... ]
  7. for(b=0;b<nbElem-1;b++){
  8. printf("%d, ",tab[b]); // j'ai ajouté une virgule apres %d
  9. }
  10. printf("%d ]",tab[nbElem-1] ); // dans la boucle for j'ai enlevé l'affichage du dernier element
  11.  
  12. }
  13. typedef int* TABLEAU;
  14. TABLEAU genereTableau(int nbElem, int valMax){
  15. TABLEAU t = (TABLEAU) malloc(nbElem * sizeof(int));
  16. if (t != NULL){
  17. int i;
  18. for (i=0; i<nbElem; i++){
  19. t[i] = rand()%(valMax + 1);
  20. }
  21.  
  22. return t;
  23. }}
  24. void tri_par_denombrement(int tab[],int nbElem, int valMax, int resultat[])
  25. {
  26.  
  27. int E[valMax+1],i,j,indice;
  28. for (i=0 ; i<valMax+1; i++)
  29. {
  30.  
  31. E[i]=0;
  32. }
  33.  
  34.  
  35. for (i=0 ; i<nbElem; i++)
  36. {
  37.  
  38. E[tab[i]]= E[tab[i]]+1;
  39.  
  40.  
  41. }
  42.  
  43. indice=0;
  44. for (i=0 ; i<valMax+1; i++)
  45. {
  46.  
  47.  
  48. for (j=0 ; j<E[i]; j++)
  49. {
  50.  
  51. resultat[indice]=i;
  52.  
  53. indice++;
  54.  
  55. }
  56.  
  57. }
  58.  
  59.  
  60. }
  61.  
  62. int taille_entier(int a)
  63. {
  64. int taille=0;
  65. while(a>0)
  66. {
  67. a=a/10;
  68. taille++;
  69.  
  70. }
  71. return taille;
  72. }
  73.  
  74. int unite(int nombre,int indice_unite)
  75. {
  76. int i, unite;
  77. for(i=0; i<indice_unite; i++)
  78. {
  79. unite=nombre%10;
  80. nombre=nombre/10;
  81.  
  82. }
  83. return unite;
  84. }
  85.  
  86. void Tri_par_base(int tab[],int valMax,int nbElem,int resultat[])
  87. {
  88. int i=0,j,h,p,c=taille_entier(valMax),tab_unite[nbElem],tab_unite_trie[nbElem];
  89. for(i=1; i<=c; i++)
  90. {
  91.  
  92. for(j=0; j<nbElem; j++) // on remplie le tableau avec les unite
  93. {
  94. tab_unite[j]=unite(tab[j],i);
  95.  
  96. }
  97.  
  98.  
  99. tri_par_denombrement(tab_unite,nbElem,valMax,tab_unite_trie); // on trie le tableau
  100.  
  101. for(h=0; h<nbElem; h++)
  102. {
  103.  
  104. for(p=0; p<nbElem; p++)
  105. {
  106.  
  107. if(tab_unite_trie[p]==tab_unite[h])
  108. {
  109. resultat[p]=tab[h];
  110. tab_unite_trie[p]=-1;
  111.  
  112. break;
  113.  
  114. }
  115. }
  116.  
  117. }
  118.  
  119.  
  120. for(j=0; j<nbElem; j++)
  121. {
  122. tab[j]=resultat[j];
  123. }
  124. }
  125.  
  126. }
  127.  
  128.  
  129.  
  130. int main()
  131. {
  132. int taille,max;
  133. printf("Veuillez saisir la taille du tableau : \n");
  134. scanf("%d",&taille);
  135. printf("Veuillez saisir le max : \n");
  136. scanf("%d",&max);
  137.  
  138. int *tab=genereTableau(taille,max);
  139.  
  140.  
  141. int resultat[taille],resultat2[taille];
  142. printf("tableau initial = ");
  143. afficheTableau(tab, taille);
  144. tri_par_denombrement(tab,taille, max,resultat);
  145. printf("\n");
  146. printf("tableau trie par denombrement = ");
  147.  
  148. afficheTableau(resultat,taille);
  149. printf("\n");
  150. printf("Tableau trie par base =");
  151.  
  152. Tri_par_base(tab,max,taille,resultat2);
  153. afficheTableau(resultat2,taille);
  154.  
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement