Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.86 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  tris
  4. //
  5. //  Created by Dylan BUTELLE on 06/04/2020.
  6. //  Copyright © 2020 Dylan BUTELLE. All rights reserved.
  7. //
  8. #include <iostream>
  9. #include <array>
  10. #include <time.h>
  11. #include <cstdlib>
  12. #include <iomanip>
  13. using namespace std;
  14. const int N=10;
  15. void init3Tab(array<int,N> & Talea,array<int,N> &Tdec,array<int,N> &Ttrie)
  16. {
  17.     int j=N-1;
  18.     for(int i=0;i<N;i++){
  19.         Talea[i]=rand()%N;
  20.         Tdec[i]=j;
  21.         Ttrie[i]=i;
  22.         j--;
  23.         }
  24.  
  25. }
  26.  
  27. void triSelection (array<int, 50> & tabI, int taille, long & cpt) {
  28.     int ind, position, maximum, i, tmp;
  29.     cpt=0;
  30.     for (ind=taille; ind>=1;ind--) { //E1
  31.         position = 0; //E2
  32.     for (i=1;i<=ind;i++) { //E3
  33.         if (tabI[position] < tabI[i]) { //E4
  34.             cpt++;
  35.             position = i; //E5
  36.         }
  37.     }
  38.     if (position != ind) { //E6
  39.         tmp = tabI[position]; //E7
  40.         tabI[position] = tabI[ind]; //E8
  41.         tabI[ind] = tmp; //E9
  42.         }
  43.     }
  44. }
  45. void triInsertion (array<int, 50> &tabI, long & cpt) {
  46.     int indice_elt, position, elt;
  47.     cpt=0;
  48.         for (indice_elt=1;indice_elt<=49;indice_elt++) { //E1
  49.             elt = tabI[indice_elt];
  50.             position = indice_elt;
  51.         while (position > 0 && tabI[position-1] > elt) { //E2
  52.             tabI[position] = tabI[position-1]; //E3
  53.             cpt++;
  54.             position=position-1;
  55.         }
  56.             tabI[position] = elt;
  57.         }
  58. }
  59. void fusion (array<int, 50> &tabI, int debut, int milieu, int fin,long & cpt) {
  60.     array<int, 50> tmp;
  61.     int cptG, cptD,i=0,j;
  62.     cptG = debut;
  63.     cptD = milieu+1;
  64.         while ((cptG<=milieu) && (cptD<=fin)) { //E1 : 2 tableaux non vides
  65.                 cpt++;
  66.             if (tabI[cptG] < tabI[cptD]) {
  67.                 tmp[i] = tabI[cptG];
  68.                 cptG = cptG+1;
  69.                 }
  70.                     else {
  71.                     tmp[i] = tabI[cptD];
  72.                     cptD = cptD+1;
  73.             }
  74.                 i = i+1;
  75.         } //un des 2 tableaux (ou les 2) est vide
  76.         while (cptD <= fin) { //E2 : tableau gauche vide, on complete avec tableau droit trie
  77.             tmp[i] = tabI[cptD];
  78.             cptD = cptD+1;
  79.             i = i+1;
  80.         }
  81.             while (cptG <= milieu) { //E3 : tableau droit vide, on complete avec tableau gauche trie
  82.                 tmp[i] = tabI[cptG];
  83.                 cptG = cptG+1;
  84.                 i = i+1;
  85.             }
  86.         for (j=0;j<=i-1;j++) { //E4 : recopie du tableau temporaire dans
  87.             tabI[debut+j] = tmp[j] ;
  88.         }
  89.     }
  90. void tri_fusion (array<int, 50> &tabI, int debut, int fin,long &cpt) {
  91.     int milieu;
  92.         if (debut < fin) {
  93.             milieu = (fin+debut)/2;
  94.             tri_fusion(tabI, debut, milieu,cpt);
  95.             tri_fusion(tabI, milieu+1, fin,cpt);
  96.             fusion(tabI, debut, milieu, fin,cpt);
  97.         }
  98.     }
  99.  
  100.  
  101. int main()
  102. {
  103.     srand(time(NULL));
  104.     array<int,N> Talea;
  105.     array<int,N> Tdec;
  106.     array<int,N> Ttrie;
  107.     long cpt;
  108.     double tps;
  109.     init3Tab(Talea,Tdec,Ttrie);
  110. //affichage cadre du tableau
  111. cout << "\t\t\t" << setw(14) << " selection " << "\t" << setw(15) << "insertion "
  112. << "\t" << setw(16) << "fusion "<< endl;
  113. cout << "\t\t\t" << setw(14) << " cpt tps " << "\t" << setw(15) << " cpt tps"
  114. << "\t" << setw(16) << " cpt tps" << endl;
  115. ///////////////////// 3 tris sur Talea
  116. cout << "Tableau aleatoire ";
  117. //appel tri selection et calcul de cpt_etape + calcul du temps d'exécution tps
  118. cout << setw(4) << "|" << setw(7) << cpt_etape << " |" << setw(7) << tps;
  119. //appel tri insertion et calcul de cpt_etape + calcul du temps d'exécution tps
  120. cout << setw(2) << "|" << setw(7) << cpt_etape << " |" << setw(7) << tps;
  121. //appel tri fusion et calcul de cpt_etape + calcul du temps d'exécution tps
  122. cout << setw(2) << "|" << setw(7) << cpt_etape << " |" << setw(7) << tps << endl;
  123. return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement