How do you write a C program to do merge sort using pthreads (parallel processin
ID: 3673338 • Letter: H
Question
How do you write a C program to do merge sort using pthreads (parallel processing), i.e. Faster than classical merge sort? How do you write a C program to do merge sort using pthreads (parallel processing), i.e. Faster than classical merge sort? How do you write a C program to do merge sort using pthreads (parallel processing), i.e. Faster than classical merge sort?Explanation / Answer
#include #include #include #define N 2 /* # of thread */ int a[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; /* target array */ /* structure for array index * used to keep low/high end of sub arrays */ typedef struct Arr { int low; int high; } ArrayIndex; void merge(int low, int high) { int mid = (low+high)/2; int left = low; int right = mid+1; int b[high-low+1]; int i, cur = 0; while(left low; aIndex[0].high = mid; aIndex[1].low = mid+1; aIndex[1].high = pa->high; if (pa->low >= pa->high) return; int i; for(i = 0; i high); pthread_exit(NULL); } int main() { ArrayIndex ai; ai.low = 0; ai.high = sizeof(a)/sizeof(a[0])-1; pthread_t thread; pthread_create(&thread, NULL, mergesort, &ai); pthread_join(thread, NULL); int i; for (i = 0; i < 10; i++) printf ("%d ", a[i]); return 0; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.