Guest User

Untitled

a guest
May 17th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <pthread.h>
  3.  
  4.  
  5.  
  6. typedef struct abc{
  7.     int *A;
  8.     int n;
  9. }abc ;
  10. void *swap( void* G){
  11.  
  12.      abc *ABC = (abc*) G;
  13.      int temp;
  14.      if(ABC->A[ABC->n] > ABC->A[ABC->n + 1]){
  15.          temp = ABC->A[ABC->n];
  16.          ABC->A[ABC->n] = ABC->A[ABC->n + 1];
  17.          ABC->A[ABC->n + 1] = temp;
  18.      }
  19.      return 0;
  20.  
  21. }
  22. void oddEvenTransSort(int *A, int n){
  23.     int m = n/2;
  24.     pthread_t *T;
  25.     abc *temp;
  26.     T = new pthread_t [m];
  27.     for (int k = 1; k < n; ++k) {
  28.  
  29.         if(k%2==0) {
  30.             for (int i = 0; i < n; i += 2) {
  31.                 temp->A=A;
  32.                 temp->n=n;
  33.                 pthread_create(&(T[i]), NULL, swap, temp);
  34.             }
  35.             for(int i = 0 ; i < n ; i+=2)
  36.             {
  37.                 pthread_join(T[i] , NULL);
  38.             }
  39.         }
  40.         else{
  41.             for (int i = 1; i < n; i += 2) {
  42.                 temp->A=A;
  43.                 temp->n=n;
  44.                 pthread_create(&(T[i]), NULL, swap, temp);
  45.             }
  46.             for(int i = 0 ; i < n ; i+=2)
  47.             {
  48.                 pthread_join(T[i] , NULL);
  49.             }
  50.         }
  51.     }
  52.  
  53. }
  54.  
  55. int main(){
  56.  
  57.     int n;
  58.     std::cout<<"enter array size";
  59.     std::cin>>n;
  60.     int A[n];
  61.     std::cout<<"enter array";
  62.     for (int i = 0; i < n; ++i) {
  63.         std::cin>>A[i];
  64.     }
  65.     oddEvenTransSort(A, n);
  66.    
  67.     for (int j = 0; j < n; ++j) {
  68.         std::cout<<A[j];
  69.     }
  70.  
  71.     return 0;
  72. }
Add Comment
Please, Sign In to add comment