Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 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. using namespace std;
  13. const int N=10;
  14. void init3Tab(array<int,N> & Talea,array<int,N> &Tdec,array<int,N> &Ttrie)
  15. {
  16.     int j=N-1;
  17.     for(int i=0;i<N;i++){
  18.         Talea[i]=rand()%N;
  19.         Tdec[i]=j;
  20.         Ttrie[i]=i;
  21.         j--;
  22.         }
  23.     }
  24. }
  25.  
  26. void triSelection (array<int, 50> & tabI, int taille, long & cpt) {
  27.     int ind, position, maximum, i, tmp;
  28.     cpt=0;
  29.     for (ind=taille; ind>=1;ind--) { //E1
  30.         position = 0; //E2
  31.     for (i=1;i<=ind;i++) { //E3
  32.         if (tabI[position] < tabI[i]) { //E4
  33.             cpt++;
  34.             position = i; //E5
  35.         }
  36.     }
  37.     if (position != ind) { //E6
  38.         tmp = tabI[position]; //E7
  39.         tabI[position] = tabI[ind]; //E8
  40.         tabI[ind] = tmp; //E9
  41.         }
  42.     }
  43. }
  44. void triInsertion (array<int, 50> &tabI, long & cpt) {
  45.     int indice_elt, position, elt;
  46.     cpt=0;
  47.         for (indice_elt=1;indice_elt<=49;indice_elt++) { //E1
  48.             elt = tabI[indice_elt];
  49.             position = indice_elt;
  50.         while (position > 0 ET tabI[position-1] > elt) { //E2
  51.             tabI[position] = tabI[position-1]; //E3
  52.             cpt++;
  53.             position=position-1;
  54.         }
  55.             tabI[position] = elt;
  56.         }
  57. }
  58. void fusion (array<int, 50> &tabI, int debut, int milieu, int fin,long & cpt) {
  59.     array<int, 50> tmp;
  60.     int cptG, cptD,i=0,j;
  61.     cptG = debut;
  62.     cptD = milieu+1;
  63.         while ((cptG<=milieu) AND (cptD<=fin)) { //E1 : 2 tableaux non vides
  64.                 cpt++;
  65.             if (tabI[cptG] < tabI[cptD]) {
  66.                 tmp[i] = tabI[cptG];
  67.                 cptG = cptG+1;
  68.                 }
  69.                     else {
  70.                     tmp[i] = tabI[cptD];
  71.                     cptD = cptD+1;
  72.             }
  73.                 i = i+1;
  74.         } //un des 2 tableaux (ou les 2) est vide
  75.         while (cptD <= fin) { //E2 : tableau gauche vide, on complete avec tableau droit trie
  76.             tmp[i] = tabI[cptD];
  77.             cptD = cptD+1;
  78.             i = i+1;
  79.         }
  80.             while (cptG <= milieu) { //E3 : tableau droit vide, on complete avec tableau gauche trie
  81.                 tmp[i] = tabI[cptG];
  82.                 cptG = cptG+1;
  83.                 i = i+1;
  84.             }
  85.         for (j=0;j<=i-1;j++) { //E4 : recopie du tableau temporaire dans
  86.             tabI[debut+j] = tmp[j] ;
  87.         }
  88.     }
  89. void tri_fusion (array<int, 50> &tabI, int debut, int fin,long &cpt) {
  90.     int milieu;
  91.         if (debut < fin) {
  92.             milieu = (fin+debut)/2;
  93.             tri_fusion(tabI, debut, milieu,cpt);
  94.             tri_fusion(tabI, milieu+1, fin,cpt);
  95.             fusion(tabI, debut, milieu, fin,cpt);
  96.         }
  97.     }
  98.  
  99.  
  100. int main()
  101. {
  102.     srand(time(NULL));
  103.  
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement