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 »
multithreaded program to sort a integer array


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 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 array is in sorted order. If the array is sorted print “Sort complete.” Otherwise print an error message. Each of the worker threads should use a bubble sort algorithm (intentionally slow, see the pseudo code below). Use an array size of 64,000 elements that you randomly initialize using rand() · 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 bubblesort algorithm. To determine how much time is required, you might use the unix time program. Simple bubble routine: for (x = 0 ; x < (="" n="" -="" 1="" );="" x++)="" {for="" (y="0" ;="" y="">< n="" -="" x="" -="" 1;="" y++)="" {if="" (array[y]=""> array[y+1]) {temp = array[y]; array[y] = array[y+1]; array[y+1] = temp; }
Answered Same DayMar 03, 2021

Answer To: 2. Develop a multithreaded program to sort an array of integers. Your program should run with 4...

Pratik answered on Mar 05 2021
132 Votes
bsort/bsort.c
// C Program to implement bubble sort using
// multi-threading
#include
#include
#include thread.h>
#include

// number of elements in array
#define MAX 64000

// number of threads
#define THREAD_MAX 4

// array 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