Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #define SIZE ( sizeof(list)/sizeof(*list) )
- #define NUM_THREADS 2
- int list[] = {7, 12, 19, 3, 18, 4, 2, -5, 6, 15, 8};
- int result[SIZE] = {0};
- typedef struct
- {
- int * subArray;
- unsigned int size;
- } SortingThreadParameters;
- typedef struct
- {
- SortingThreadParameters left;
- SortingThreadParameters right;
- } MergingThreadParameters;
- SortingThreadParameters *paramsLeft = malloc( sizeof( SortingThreadParameters ) );
- paramsLeft->subArray = list;
- paramsLeft->size = SIZE/2;
- SortingThreadParameters *paramsRight = malloc( sizeof( SortingThreadParameters ) );
- paramsRight->subArray = list + paramsLeft->size;
- paramsRight->size = SIZE - paramsLeft->size;
- MergingThreadParameters *paramsMerge = malloc( sizeof( MergingThreadParameters ) );
- paramsMerge->left = *paramsLeft;
- paramsMerge->right = *paramsRight;
- void bubble_sort(SortingThreadParameters *t) {
- int arr[] = t->subArray;
- int n = t->size;
- int temp;
- int i;
- int j;
- i = 0;
- while (i < n) {
- j = 0;
- while (j < i) {
- if (arr[j] > arr[i]) {
- temp = arr[j];
- arr[j] = arr[i];
- arr[i] = temp;
- }
- j++;
- }
- i++;
- }
- }
- int main()
- {
- pthread_t threads[2];
- pthread_create(&threads[0], NULL, bubble_sort, paramsLeft);
- pthread_create(&threads[1], NULL, bubble_sort, paramsRight);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement