Question: Write a multithreaded sorting program that works as follows: A list of integers is divided into two smaller lists of equal size. Two separate threads
Write a multithreaded sorting program that works as follows: A list of integers is divided into two smaller lists of equal size. Two separate threads (which we will term sorting threads) sort each sublist using a sorting algorithm of your choice. The two sublists are then merged by a third threada merging thread which merges the two sublists into a single sorted list. Because global data are shared across all threads, perhaps the easiest way to set up the data is to create a global array. Each sorting thread will work on one half of this array. A second global array of the same size as the unsorted integer array will also be established. The merging thread will then merge the two sublists into this second array. Graphically, this program is structured according to Figure 4.20. This programming project will require passing parameters to each of the sorting threads. In particular, it will be necessary to identify the starting index from which each thread is to begin sorting. Refer to the instructions in Project 1 for details on passing parameters to a thread. The parent thread will output the sorted array once all sorting threads have exited. Program Input: number of elements (multiple of 2) passed on the command line. The arrays should be created dynamically and the numbers in the list should be generated randomly using the rand( ) function. You may want to limit the range between 0 and 1000. Program Input: the sorted array.
Task 2 Modify your program to allow the user to enter the number of threads (two or four) on the command line, as well.
I am using this code from http://www.chegg.com/homework-help/questions-and-answers/write-multithreaded-sorting-program-c-works-follows-list-integers-divided-two-smaller-list-q512501
However, I need to include user input to declare the size of the array and fill it with random numbers from 1-1000. I am not sure how to pass this parameters to the sort and merge method since in the code example I linked has everything declared globally.
Figure 4.20
Write a multithreaded sorting program that works as follows: A list of integers is divided into two smaller lists of equal size. Two separate threads (which we will term sorting threads) sort each sublist using a sorting algorithm of your choice. The two sublists are then merged by a third thread-a merging thread-which merges the two sublists into a sin gle sorted list. Because global data are shared across all threads, perhaps the easiest way to set up the data is to create a global array. Each sorting thread w work on one half of this array. A second global array of the same size as the unsorted integer array will also be established. The merging thread will then merge the two sublists into this second array. Graphically, this program is structured according to Figure 4.20. This programming project will require passing parameters to each of the sorting threads. In particular, it will be necessary to identify the starting index from which each thread is to begin sorting. Refer to the instructions in Project 1 for details on passing parameters to a thread. The parent thread will output the sorted array once al sorting threads have exited Program Input number of elements (multiple of 2) passed on the command line. The arrays should be created dynamically and the numbers in the list should be generated randomly using the ran d()function. You may want to limit the range between 0 and 1000 Program Input: the sorted array. 7, 12, 19,3 18, 4,2, 6, 15 8 7, 12, 19, 3, 18 4,2,6, 15, 8 Merge Thread 2, 3, 4,6,7,8, 12, 15, 18, 19 Sorted List Figure 4.20 Multithreaded sorting. Task 2- Modify your program to allow the user to enter the number ofthreads (two or four) on the command line, as we
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
