Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

2. Develop a multithreaded program to sort an array of integers. Your program should run with 4 threads o Break the array into four equally sized partitions o Sort the partitions separately in...

1 answer below »
2. Develop a multithreaded program to sort an a
ay of integers.

Your program should run with 4 threads
o
Break the a
ay into four equally sized partitions
o
Sort the partitions separately in different threads
o
Merge the results back together. This is a 2 step process where two threads merge the
four partitions into two partitions and then with one thread merge the two partitions
into one.
o
Write a simple routine that checks the final a
ay is in sorted order. If the a
ay is
sorted print “Sort complete.” Otherwise print an e
or message.

Each of the worker threads should use a bu
le sort algorithm (intentionally slow, see the
pseudo code below).

Use an a
ay size of 64,000 elements that you randomly initialize using
and()
·
The final version of your program should
only print out the simple message generated by your simple checker.
·
Along with your code, turn in a comparison of how much time the multithread program takes
versus running the same problem using a 1 threaded bu
lesort algorithm. To determine how
much time is required, you might use the unix time program.

Simple bu
le routine:
for (x = 0 ; x < ( n - 1 ); x++)
{for (y = 0 ; y < n - x - 1; y++)
{if (a
ay[y] > a
ay[y+1])
{temp = a
ay[y];
a
ay[y] = a
ay[y+1];
a
ay[y+1] = temp;
}
Answered Same Day Mar 03, 2021

Solution

Pratik answered on Mar 05 2021
147 Votes
bsort
sort.c
C Program to implement bu
le sort using
multi-threading
#include
#include #include
#include

number of elements in a
ay
#define MAX 64000

number of threads
#define THREAD_MAX 4

a
ay of size MAX
int a[MAX];
int part = 0;

merge function for merging two parts
void merge(int low, int mid, int high)
{
int* left = (int *)malloc((mid - low + 1)*sizeof(int));
int* right = (int *)malloc((high-mid)*sizeof(int));


n1 is size of left part and n2 is size

of right part
int n1 = mid - low + 1, n2 = high - mid, i, j;


storing values in left part
for (i = 0; i < n1; i++)
left[i] = a[i + low];


storing values in right part
for (i = 0; i < n2; i++)
right[i] = a[i + mid + 1];

int k = low;
i = j = 0;


merge left and right in ascending order
while (i < n1 && j < n2) {
if (left[i] <= right[j])
a[k++] = left[i++];
else
a[k++] = right[j++];
}


insert remaining values from left
while (i < n1) {
a[k++] = left[i++];
}


insert remaining values from right
while (j < n2) {
a[k++] = right[j++];
}...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here