Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <thread>
  3.  
  4. using namespace std;
  5.  
  6. struct argumento{
  7.     int ini, fin, mid;
  8. };
  9. pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;
  10.  
  11. int A[101010];
  12. void merge(int inicio, int meio, int fim){ //função merge normal
  13.     int i=0, j=0, k=0;
  14.     int temp[100000], p1, p2, tam;
  15.     int fim1=0, fim2=0;
  16.     tam = fim-inicio+1;
  17.     p1 = inicio;
  18.     p2=meio+1;
  19.     for(i=0;i<tam;i++){
  20.         if(!fim1 && !fim2){
  21.             if(A[p1]<A[p2]){
  22.                 temp[i] = A[p1++];
  23.             }
  24.             else{
  25.                 temp[i] = A[p2++];
  26.             }
  27.             if(p1>meio){
  28.                 fim1 = 1;
  29.             }
  30.             if(p2>fim){
  31.                 fim2 = 1;
  32.             }
  33.         }
  34.         else{
  35.             if(!fim1){
  36.                 temp[i] = A[p1++];
  37.             }
  38.             else{
  39.                 temp[i] = A[p2++];
  40.             }
  41.         }
  42.     }
  43.     for(j=0,k=inicio;j<tam;j++,k++){
  44.         A[k] = temp[j];
  45.     }
  46. }
  47.  
  48. void *merge_sort(void *argv){ //thread merge sort
  49.     int inicio, meio, fim;
  50.     argumento *argx = (argumento*)argv; //recebo o argumento e transfiro os valores
  51.     inicio = argx->ini;
  52.     fim = argx->fin;  
  53.     if(inicio<fim){
  54.         meio = (inicio+fim)/2;
  55.         pthread_t thread1, thread2;
  56.         argumento *arg = new argumento; //declaro os argumentos das novas threads
  57.         argumento *arg2 = new argumento;
  58.         //merge_sort(inicio,meio);
  59.         arg->ini = inicio; //escolho os argumentos das novas threads
  60.         arg->fin = meio;
  61.         arg2->ini = meio+1;
  62.         arg2->fin = fim;
  63.         pthread_create(&thread1, NULL, merge_sort, (void*)arg);
  64.        
  65.         //merge_sort(meio+1,fim);
  66.         pthread_create(&thread2, NULL, merge_sort, (void*)arg2);
  67.        
  68.         pthread_join(thread1, NULL); //espero as threads
  69.         pthread_join(thread2, NULL);
  70.        
  71.         //merge(inicio,meio,fim);
  72.         merge(inicio, meio, fim);
  73.        
  74.     }
  75.     pthread_exit(NULL);
  76. }
  77.  
  78. int main() {
  79.     cout << "Digite o tamanho do array: ";
  80.     argumento *arg = new argumento; // Declaro o argumento
  81.     cin >> arg->fin; //leio o tamanho do array
  82.     printf("Digite os valores do array:\n-> ");
  83.     int i;
  84.     for(i=0;i<arg->fin;i++){
  85.         cin >> A[i];
  86.     }
  87.     pthread_t thread;
  88.     arg->fin--; //atualizo os valores do argumento
  89.     arg->ini=0;
  90.     pthread_create(&thread, NULL, merge_sort, (void*)arg); //crio a thread
  91.     //merge_sort(A, 0, An);
  92.     pthread_join(thread, NULL);//espero ela terminar
  93.     cout << "O array ordenado eh:\n-> ";
  94.     for(i=0;i<=arg->fin;i++){
  95.         cout << A[i] << " "; // printo o array ordenado
  96.     }
  97.     cout << endl;
  98.     return 0; // FIM
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement