In C, using this interface:  void setSortThreads(int count); void sortThreaded(char** array, unsigned int count);     How can you modify the following quicksort program in order to use multithreading, with the goal of performing faster than qsort for large data sets? Use basic parallelization and limit the number of threads to the amount held by the global variable maximumThreads, specified by the user (when they call setSortThreads in the main function). If possible it would be helpful to test the code on a big text file, like maybe the dictionary. I'll include a sample main function at the end that I have been using. You would need your own text file to test it on. #include #include #include #include #include #define SORT_THRESHOLD 40 typedef struct _sortParams { char** array; int left; int right; } SortParams; static int maximumThreads; /* maximum # of threads to be used */ /* This is an implementation of insert sort, which although it is */ /* n-squared, is faster at sorting short lists than quick sort, */ /* due to its lack of recursive procedure call overhead. */ static void insertSort(char** array, int left, int right) { int i, j; for (i = left + 1; i <= right; i++) { char* pivot = array[i]; j = i - 1; while (j >= left && (strcmp(array[j],pivot) > 0)) { array[j + 1] = array[j]; j--; } array[j + 1] = pivot; } } /* Recursive quick sort, but with a provision to use */ /* insert sort when the range gets small. */ static void quickSort(void* p) { SortParams* params = (SortParams*) p; char** array = params->array; int left = params->left; int right = params->right; int i = left, j = right; if (j - i > SORT_THRESHOLD) { /* if the sort range is substantial, use quick sort */ int m = (i + j) >> 1; /* pick pivot as median of */ char* temp, *pivot; /* first, last and middle elements */ if (strcmp(array[i],array[m]) > 0) { temp = array[i]; array[i] = array[m]; array[m] = temp; } if (strcmp(array[m],array[j]) > 0) { temp = array[m]; array[m] = array[j]; array[j] = temp; if (strcmp(array[i],array[m]) > 0) { temp = array[i]; array[i] = array[m]; array[m] = temp; } } pivot = array[m]; for (;;) { while (strcmp(array[i],pivot) < 0) i++; /* move i down to first element greater than or equal to pivot */ while (strcmp(array[j],pivot) > 0) j--; /* move j up to first element less than or equal to pivot */ if (i < j) { char* temp = array[i]; /* if i and j have not passed each other */ array[i++] = array[j]; /* swap their respective elements and */ array[j--] = temp; /* advance both i and j */ } else if (i == j) { i++; j--; } else break; /* if i > j, this partitioning is done */ } SortParams first; first.array = array; first.left = left; first.right = j; quickSort(&first); /* sort the left partition */ SortParams second; second.array = array; second.left = i; second.right = right; quickSort(&second); /* sort the right partition */ } else insertSort(array,i,j); /* for a small range use insert sort */ } /* user interface routine to set the number of threads sortT is permitted to use */ void setSortThreads(int count) { maximumThreads = count; } /* user callable sort procedure, sorts array of count strings, beginning at address array */ void sortThreaded(char** array, unsigned int count) { SortParams parameters; parameters.array = array; parameters.left = 0; parameters.right = count - 1; quickSort(¶meters); }   int compareStrings(const void *a, const void *b) { return strcmp(*(const char **)a, *(const char **)b); } int main() { // Open the file FILE *file = fopen("lorem", "r"); if (file == NULL) { printf("Could not open file.\n"); return 1; } // Estimate the number of words or lines; you can adapt this based on your file int estimatedCount = 1000; char **array = malloc(estimatedCount * sizeof(char *)); char buffer[100]; // assuming each word or line won't exceed 100 characters unsigned int count = 0; while (fscanf(file, "%s", buffer) != EOF) { // reading word by word array[count] = strdup(buffer); count++; if (count >= estimatedCount) { estimatedCount *= 2; array = realloc(array, estimatedCount * sizeof(char *)); } } fclose(file); // Duplicate array for qsort char *qsortArray[count]; for (int i = 0; i < count; ++i) { qsortArray[i] = array[i]; } // Set number of threads setSortThreads(4); // Time sortThreaded clock_t start = clock(); sortThreaded(array, count); clock_t end = clock(); double sortThreaded_time = ((double) (end - start)) / CLOCKS_PER_SEC; // Time qsort start = clock(); qsort(qsortArray, count, sizeof(char *), compareStrings); end = clock(); double qsort_time = ((double) (end - start)) / CLOCKS_PER_SEC; // Output timing results printf("Time taken by sortThreaded: %e seconds\n", sortThreaded_time); printf("Time taken by qsort: %e seconds\n", qsort_time); // Validate that the array is sorted for (int i = 0; i < count - 1; ++i) { if (strcmp(array[i], array[i + 1]) > 0) { printf("sortThreaded failed at index %d\n", i); return 1; } } printf("Array sorted successfully by sortThreaded!\n"); // Free allocated memory for strings for (int i = 0; i < count; ++i) { free(array[i]); } free(array); return 0; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

In C, using this interface: 

void setSortThreads(int count);
void sortThreaded(char** array, unsigned int count);
 
 
How can you modify the following quicksort program in order to use multithreading, with the goal of performing faster than qsort for large data sets? Use basic parallelization and limit the number of threads to the amount held by the global variable maximumThreads, specified by the user (when they call setSortThreads in the main function). If possible it would be helpful to test the code on a big text file, like maybe the dictionary. I'll include a sample main function at the end that I have been using. You would need your own text file to test it on.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <time.h>

#define SORT_THRESHOLD 40

typedef struct _sortParams {
char** array;
int left;
int right;
} SortParams;

static int maximumThreads; /* maximum # of threads to be used */

/* This is an implementation of insert sort, which although it is */
/* n-squared, is faster at sorting short lists than quick sort, */
/* due to its lack of recursive procedure call overhead. */

static void insertSort(char** array, int left, int right) {
int i, j;
for (i = left + 1; i <= right; i++) {
char* pivot = array[i];
j = i - 1;
while (j >= left && (strcmp(array[j],pivot) > 0)) {
array[j + 1] = array[j];
j--;
}
array[j + 1] = pivot;
}
}

/* Recursive quick sort, but with a provision to use */
/* insert sort when the range gets small. */

static void quickSort(void* p) {
SortParams* params = (SortParams*) p;
char** array = params->array;
int left = params->left;
int right = params->right;
int i = left, j = right;

if (j - i > SORT_THRESHOLD) { /* if the sort range is substantial, use quick sort */

int m = (i + j) >> 1; /* pick pivot as median of */
char* temp, *pivot; /* first, last and middle elements */
if (strcmp(array[i],array[m]) > 0) {
temp = array[i]; array[i] = array[m]; array[m] = temp;
}
if (strcmp(array[m],array[j]) > 0) {
temp = array[m]; array[m] = array[j]; array[j] = temp;
if (strcmp(array[i],array[m]) > 0) {
temp = array[i]; array[i] = array[m]; array[m] = temp;
}
}
pivot = array[m];

for (;;) {
while (strcmp(array[i],pivot) < 0) i++; /* move i down to first element greater than or equal to pivot */
while (strcmp(array[j],pivot) > 0) j--; /* move j up to first element less than or equal to pivot */
if (i < j) {
char* temp = array[i]; /* if i and j have not passed each other */
array[i++] = array[j]; /* swap their respective elements and */
array[j--] = temp; /* advance both i and j */
} else if (i == j) {
i++; j--;
} else break; /* if i > j, this partitioning is done */
}

SortParams first; first.array = array; first.left = left; first.right = j;
quickSort(&first); /* sort the left partition */

SortParams second; second.array = array; second.left = i; second.right = right;
quickSort(&second); /* sort the right partition */

} else insertSort(array,i,j); /* for a small range use insert sort */
}

/* user interface routine to set the number of threads sortT is permitted to use */

void setSortThreads(int count) {
maximumThreads = count;
}

/* user callable sort procedure, sorts array of count strings, beginning at address array */

void sortThreaded(char** array, unsigned int count) {
SortParams parameters;
parameters.array = array; parameters.left = 0; parameters.right = count - 1;
quickSort(&parameters);
}
 
int compareStrings(const void *a, const void *b) {
return strcmp(*(const char **)a, *(const char **)b);
}

int main() {
// Open the file
FILE *file = fopen("lorem", "r");
if (file == NULL) {
printf("Could not open file.\n");
return 1;
}

// Estimate the number of words or lines; you can adapt this based on your file
int estimatedCount = 1000;
char **array = malloc(estimatedCount * sizeof(char *));
char buffer[100]; // assuming each word or line won't exceed 100 characters
unsigned int count = 0;

while (fscanf(file, "%s", buffer) != EOF) { // reading word by word
array[count] = strdup(buffer);
count++;
if (count >= estimatedCount) {
estimatedCount *= 2;
array = realloc(array, estimatedCount * sizeof(char *));
}
}

fclose(file);

// Duplicate array for qsort
char *qsortArray[count];
for (int i = 0; i < count; ++i) {
qsortArray[i] = array[i];
}

// Set number of threads
setSortThreads(4);

// Time sortThreaded
clock_t start = clock();
sortThreaded(array, count);
clock_t end = clock();
double sortThreaded_time = ((double) (end - start)) / CLOCKS_PER_SEC;

// Time qsort
start = clock();
qsort(qsortArray, count, sizeof(char *), compareStrings);
end = clock();
double qsort_time = ((double) (end - start)) / CLOCKS_PER_SEC;

// Output timing results
printf("Time taken by sortThreaded: %e seconds\n", sortThreaded_time);
printf("Time taken by qsort: %e seconds\n", qsort_time);

// Validate that the array is sorted
for (int i = 0; i < count - 1; ++i) {
if (strcmp(array[i], array[i + 1]) > 0) {
printf("sortThreaded failed at index %d\n", i);
return 1;
}
}

printf("Array sorted successfully by sortThreaded!\n");

// Free allocated memory for strings
for (int i = 0; i < count; ++i) {
free(array[i]);
}
free(array);

return 0;
}
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 6 steps with 5 images

Blurred answer
Knowledge Booster
Race Condition
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education