Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.42 KB | None | 0 0
  1. #include <pthread.h>
  2. #define SIZE ( sizeof(list)/sizeof(*list) )
  3. #define NUM_THREADS 2
  4.  
  5. int list[] = {7, 12, 19, 3, 18, 4, 2, -5, 6, 15, 8};
  6. int result[SIZE] = {0};
  7.  
  8. typedef struct
  9. {
  10.     int * subArray;
  11.     unsigned int size;
  12. } SortingThreadParameters;
  13.  
  14. typedef struct
  15. {
  16.     SortingThreadParameters left;
  17.     SortingThreadParameters right;
  18. } MergingThreadParameters;
  19.  
  20. SortingThreadParameters *paramsLeft = malloc( sizeof( SortingThreadParameters ) );
  21. paramsLeft->subArray = list;
  22. paramsLeft->size = SIZE/2;
  23.  
  24. SortingThreadParameters *paramsRight = malloc( sizeof( SortingThreadParameters ) );
  25. paramsRight->subArray = list + paramsLeft->size;
  26. paramsRight->size = SIZE - paramsLeft->size;
  27.  
  28. MergingThreadParameters *paramsMerge = malloc( sizeof( MergingThreadParameters ) );
  29. paramsMerge->left = *paramsLeft;
  30. paramsMerge->right = *paramsRight;
  31.  
  32. void bubble_sort(SortingThreadParameters *t) {
  33.     int arr[] = t->subArray;
  34.     int n = t->size;
  35.     int temp;
  36.     int i;
  37.     int j;
  38.  
  39.     i = 0;
  40.     while (i < n) {
  41.         j = 0;
  42.         while (j < i) {
  43.             if (arr[j] > arr[i]) {
  44.             temp = arr[j];
  45.             arr[j] = arr[i];
  46.             arr[i] = temp;
  47.             }
  48.             j++;
  49.         }
  50.         i++;
  51.     }
  52. }
  53.  
  54. int main()
  55. {
  56.     pthread_t threads[2];
  57.     pthread_create(&threads[0], NULL, bubble_sort, paramsLeft);
  58.     pthread_create(&threads[1], NULL, bubble_sort, paramsRight);
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement