Assume you have an array arr[0,1,…n-1] to sort and want to use p number of threads to do so.

For the sake of simplicity, let us assume that the smallest element has a value of 0 and the largest has a value of m. If the smallest is not equal to 0, then the values can be scaled while assigning elements to threads.

Divide your array into p chunks, each with at most floor(m/p) + 1 different element value.

The **i**th chunk consists of elements arr[j] such that:

(i — 1) * (floor(m/p) + 1) <= arr[j] < i * (floor(m/p) + 1)

For example

arr[0..9] = {5, 4, 1, 3, 7, 0, 1, 6, 3, 2} 

Here, p= 3, then m = 7

The above array has to be divided into 3 chunks, But how?

When i = 1
(1 - 1)*  (floor(7/3) + 1) <= arr[j] < 1 * (floor(7/3)+1)
0 <= arr[j] < 3
[0, 3)
When i = 2
(2 - 1)*  (floor(7/3) + 1) <= arr[j] < 2* (floor(7/3)+1)
3 <= arr[j] < 6
[3, 6)
When i = 3
(1 - 1)*  (floor(7/3) + 1) <= arr[j] < 1 * (floor(7/3)+1)
6 <= arr[j] < 9
[6, 9)

Then, assign each chunk to a different thread. Each thread sorts one chunk, and to sort the entire array, simply concatenate the results from all threads in the following order:

thread_1 thread_2 ... thread_p

Time Complexity

The complexity of the counting sort, as you may realize, is O(n + L), where n is the number of elements to sort and L is the largest value of an element.

To begin, keep in mind that you can scale down values in each thread so that L< floor(m/p) + 1 in that thread, so the complexity of count sort in each thread is always proportional to the number of elements in that thread.

#parallel-computing #threads #sorting-algorithms #algorithms

Paralleling Counting Sort using Multi-Threads and Maintaining the Stability
1.10 GEEK