The problem concerns sorting a large array in-memory using concurrent threads. One approach is the
following:
(a) partition the array into N chunks of near-equal size, where N is a power of two.
(b) in parallel, sort each chunk, using a standard sorting technique, e.g. insertion sort.
(c) while there is more than one chunk, merge pairs of chunks in parallel.
For example, suppose the original array contains 16 entries, and we want to use up to N=4 threads.
Initial array: [8, 9, 2, 5, 11, 4, 19, 7, 13, 6, 16, 3, 19, 1, 10, 14]
We subdivide the array into four (= 16/4) chunks; in practice this does not involve copying the array, but
rather allocating a range of indices to each of four threads:
Array chunks: [[8, 9, 2, 5], [11, 4, 19, 7], [13, 6, 16, 3], [19, 1, 10, 14]]
Next, each thread sorts its chunk in-place.
Sorted chunks: [[2, 5, 8, 9], [4, 7, 11, 19], [3, 6, 13, 16], [1, 10, 14, 19]]
There are 4 > 1 chunks, so we perform a merge pass, using two threads to merge two pairs of adjacent
chunks into larger ordered chunks:
First merge pass: [[2, 4, 5, 7, 8, 9, 11, 19], [1, 3, 6, 10, 13, 14, 16, 19]]
There are still 2 > 1 chunks, so we perform a further merge pass, now using a single thread to merge the
remaining chunks:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 16, 19]
The original array is now sorted.
To simplify the programming task, you can assume that the size of your input array and the number of
threads are both powers of 2. You program should create an array dynamically (219 should be a decent
size), populate the array with random values, and then sort it. For the “merge” phase, you will find it useful to
create a second array, of the same size as the first, and store the merged chunks in the merge array. Then
swap the merge array with the original array. Once sorting is complete, your program should check that the
array is sorted within the main body (using a simple sequential scan), before reporting the result of the check
on the terminal.
The number of threads to use can be fixed as a constant in your program. I recommend running 8 threads
as a starting point