Parallel Count Sort
Last Updated :
24 Mar, 2023
What is Parallel Count Sort?
Parallel count sort is an efficient algorithm that sorts an array of elements in a parallel manner. It is a variation of the classic count sort algorithm which is used to sort a collection of objects based on their frequency. The algorithm is based on the idea of counting the number of elements in a particular range and then sorting the elements according to their frequency.
Parallel count sort is a fast and efficient sorting algorithm that is suitable for applications that require a large amount of data to be sorted. It is especially useful for sorting large datasets. The algorithm is based on the idea of counting the number of elements in a particular range and then sorting the elements according to their frequency. The main advantage of the parallel count sort is that it can be implemented in a distributed system and can be used to sort large amounts of data quickly.
Example:
Input: arr[] =[ 5, 3, 4, 6, 1, 2, 7, 9, 8 ]
Output: sorted array: [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
Assumptions for the Algorithm:
The algorithm makes some assumptions about the data that is being sorted. These assumptions are:
- The data is discrete and can be represented as integers.
- The data is distributed uniformly across the range of values that are being sorted.
- The data is evenly distributed across the range of values that are being sorted.
- The data is sorted in ascending order.
Algorithmic Approach:
The algorithm works by counting the number of elements in a particular range and then sorting the elements according to their frequency. The algorithm starts by counting the number of elements in a particular range and then sorting the elements according to their frequency.
Illustrations:
Consider an array arr[] = { 5, 3, 4, 6, 1, 2, 7, 9, 8};
Follow the below steps to solve the above approach:
- Count the number of elements in each range.
- For example, in the above array, the number of elements in the range 1 to 3 is 3 (1, 2, 3). The number of elements in the range 3 to 5 is 2 (4, 5). The number of elements in the range 6 to 8 is 3 (6, 7, 8). The number of elements in the range of 9 to 10 is 1 (9).
- Sort the elements in each range according to their frequency.
- For example, in the above array, the elements in the range 1 to 3 will be sorted in ascending order (1, 2, 3). The elements in the range 3 to 5 will be sorted in ascending order (4, 5). The elements in the range 6 to 8 will be sorted in ascending order (6, 7, 8). The element in the range 9 to 10 will be sorted in ascending order (9).
- Merge the sorted elements in each range.
- For example, in the above array, the sorted elements in the range 0 to 2 will be merged with the sorted elements in the range 3 to 5 (1, 2, 3, 4, 5). The sorted elements in the range 6 to 8 will be merged with the sorted elements in the range 9 to 10 (6, 7, 8, 9).
- The merged elements are now sorted in ascending order.
- The merged elements will be sorted in ascending order (1, 2, 3, 4, 5, 6, 7, 8, 9).
Below is the implementation of the above approach:
C++
#include <iostream>
#include <omp.h>
#include <vector>
using namespace std;
int getMax(vector< int > arr, int n)
{
int mx = arr[0];
for ( int i = 1; i < n; i++)
if (arr[i] > mx)
mx = arr[i];
return mx;
}
void countSort(vector< int >& arr, int n, int exp )
{
int output[n];
int i, count[10] = { 0 };
for (i = 0; i < n; i++)
count[(arr[i] / exp ) % 10]++;
for (i = 1; i < 10; i++)
count[i] += count[i - 1];
for (i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp ) % 10] - 1] = arr[i];
count[(arr[i] / exp ) % 10]--;
}
for (i = 0; i < n; i++)
arr[i] = output[i];
}
void parallelCountSort(vector< int >& arr, int n)
{
int m = getMax(arr, n);
for ( int exp = 1; m / exp > 0; exp *= 10) {
for ( int i = 0; i < n; i++)
countSort(arr, n, exp );
}
}
int main()
{
vector< int > arr = { 170, 45, 75, 90, 802, 24, 2, 66 };
int n = arr.size();
cout << "Array before sorting: \n" ;
for ( int i = 0; i < n; i++) {
cout << arr[i] << " " ;
}
cout << "\n" ;
parallelCountSort(arr, n);
cout << "Array after sorting: \n" ;
for ( int i = 0; i < n; i++) {
cout << arr[i] << " " ;
}
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int getMax( int arr[], int n)
{
int mx = arr[ 0 ];
for ( int i = 1 ; i < n; i++)
if (arr[i] > mx)
mx = arr[i];
return mx;
}
public static void countSort( int arr[], int n, int exp)
{
int output[] = new int [n];
int i;
int count[] = new int [ 10 ];
for (i = 0 ; i < n; i++)
count[(arr[i] / exp) % 10 ]++;
for (i = 1 ; i < 10 ; i++)
count[i] += count[i - 1 ];
for (i = n - 1 ; i >= 0 ; i--) {
output[count[(arr[i] / exp) % 10 ] - 1 ] = arr[i];
count[(arr[i] / exp) % 10 ]--;
}
for (i = 0 ; i < n; i++)
arr[i] = output[i];
}
public static void parallelCountSort( int arr[], int n)
{
int m = getMax(arr, n);
for ( int exp = 1 ; m / exp > 0 ; exp *= 10 ) {
for ( int i = 0 ; i < n; i++)
countSort(arr, n, exp);
}
}
public static void main(String[] args)
{
int arr[] = { 170 , 45 , 75 , 90 , 802 , 24 , 2 , 66 };
int n = arr.length;
System.out.print( "Array before sorting: \n" );
for ( int i = 0 ; i < n; i++) {
System.out.print(arr[i] + " " );
}
System.out.println();
parallelCountSort(arr, n);
System.out.print( "Array after sorting: \n" );
for ( int i = 0 ; i < n; i++) {
System.out.print(arr[i] + " " );
}
}
}
|
Python3
from typing import List
def get_max(arr: List [ int ]) - > int :
mx = arr[ 0 ]
for i in range ( 1 , len (arr)):
if arr[i] > mx:
mx = arr[i]
return mx
def count_sort(arr: List [ int ], exp: int ):
output = [ 0 ] * len (arr)
count = [ 0 ] * 10
for i in range ( len (arr)):
count[(arr[i] / / exp) % 10 ] + = 1
for i in range ( 1 , 10 ):
count[i] + = count[i - 1 ]
for i in range ( len (arr) - 1 , - 1 , - 1 ):
output[count[(arr[i] / / exp) % 10 ] - 1 ] = arr[i]
count[(arr[i] / / exp) % 10 ] - = 1
for i in range ( len (arr)):
arr[i] = output[i]
def parallel_count_sort(arr: List [ int ]):
m = get_max(arr)
exp = 1
while m / / exp > 0 :
for i in range ( len (arr)):
count_sort(arr, exp)
exp * = 10
def main():
arr = [ 170 , 45 , 75 , 90 , 802 , 24 , 2 , 66 ]
print ( "Array before sorting:" )
print (arr)
parallel_count_sort(arr)
print ( "Array after sorting:" )
print (arr)
if __name__ = = "__main__" :
main()
|
C#
using System;
public class GFG {
public static int getMax( int [] arr, int n)
{
int mx = arr[0];
for ( int i = 1; i < n; i++)
if (arr[i] > mx)
mx = arr[i];
return mx;
}
public static void countSort( int [] arr, int n, int exp)
{
int [] output = new int [n];
int i;
int [] count = new int [10];
for (i = 0; i < n; i++)
count[(arr[i] / exp) % 10]++;
for (i = 1; i < 10; i++)
count[i] += count[i - 1];
for (i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
for (i = 0; i < n; i++)
arr[i] = output[i];
}
public static void parallelCountSort( int [] arr, int n)
{
int m = getMax(arr, n);
for ( int exp = 1; m / exp > 0; exp *= 10) {
for ( int i = 0; i < n; i++)
countSort(arr, n, exp);
}
}
static public void Main()
{
int [] arr = { 170, 45, 75, 90, 802, 24, 2, 66 };
int n = arr.Length;
Console.Write( "Array before sorting: \n" );
for ( int i = 0; i < n; i++) {
Console.Write(arr[i] + " " );
}
Console.Write( "\n" );
parallelCountSort(arr, n);
Console.Write( "Array after sorting: \n" );
for ( int i = 0; i < n; i++) {
Console.Write(arr[i] + " " );
}
}
}
|
Javascript
function getMax(arr, n)
{
let mx = arr[0];
for (let i = 1; i < n; i++)
if (arr[i] > mx)
mx = arr[i];
return mx;
}
function countSort(arr, n, exp)
{
let output= new Array(n);
let count= new Array(10).fill(0);
let x = Math.floor(arr[i] / exp);
for (let i = 0; i < n; i++)
count[x % 10]++;
for (let i = 1; i < 10; i++)
count[i] += count[i - 1];
for (let i = n - 1; i >= 0; i--) {
output[count[x % 10] - 1] = arr[i];
count[x % 10]--;
}
for (let i = 0; i < n; i++)
arr[i] = output[i];
}
function parallelCountSort(arr, n)
{
let m = getMax(arr, n);
for (let exp = 1; m / exp > 0; exp *= 10) {
for (let i = 0; i < n; i++)
countSort(arr, n, exp);
}
}
let arr = [ 170, 45, 75, 90, 802, 24, 2, 66 ];
let n = arr.length;
console.log( "Array before sorting: <br>" );
for (let i = 0; i < n; i++) {
console.log(arr[i] + " " );
}
console.log( "<br>" );
parallelCountSort(arr, n);
console.log( "Array after sorting: <br>" );
for (let i = 0; i < n; i++) {
console.log(arr[i] + " " );
}
|
Output
Array before sorting:
170 45 75 90 802 24 2 66
Array after sorting:
2 24 45 66 75 90 170 802
Time Complexity: O(NlogN)
Auxiliary space: O(N)
The difference with Sequential Count Sort:
- The main difference between the parallel count sort and the sequential count sort is that the parallel count sort is much faster than the sequential count sort. This is because the parallel count sort is able to process large amounts of data in parallel. The sequential count sort, on the other hand, can only process data sequentially.
- Another difference between the two algorithms is that the parallel count sort is more suitable for distributed systems, while the sequential count sort is better suited for single-machine systems.
- The parallel count sort is also more suitable for sorting large datasets as it can process data in parallel.
Conclusion:
In conclusion, the parallel count sort is a fast and efficient sorting algorithm that is suitable for applications that require a large amount of data to be sorted. It is especially useful for sorting large datasets. The algorithm is based on the idea of counting the number of elements in a particular range and then sorting the elements according to their frequency. The main advantage of the parallel count sort is that it can be implemented in a distributed system and can be used to sort large amounts of data quickly.
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...