Article Directory
In this tutorial, you will learn how cardinal sorting works. In addition, you will find examples in C language.
Cardinality sorting is a sorting technique, which first groups the numbers in the same position to sort the elements. Then, sort the elements according to their increasing/decreasing order.
Suppose we have an array of 8 elements. First, we will sort the elements according to the value of the ones place. Then, we will sort the elements according to the value of the tens place. This process continues until the last valid digit.
The initial array is [121, 432, 564, 23, 1, 45, 788]. Sort by radix, as shown in the figure below.
Before reading this article, please take a look at counting sorting first, because counting sorting is used as an intermediate sorting in radix sorting.
How does radix sorting work?
- Find the largest element in the array, which is max. Let X be the number of digits in max. X is calculated because we have to traverse all valid bits of all elements.
In the array [121,432,564,23,1,45,788], we have the largest number 788. It has 3 numbers. Therefore, the loop should be executed to the hundredth place (3 times). - Now, check each valid digit one by one.
Any stable sorting technique can be used to sort the numbers in each valid position. We used the counting and sorting method.
Sort the elements according to the ones digit.
- Now, sort the elements according to ten digits.
- Finally, sort the elements according to the hundred digits.
Radix sorting algorithm
radixSort(array)
d <- maximum number of digits in the largest element
create d buckets of size 0-9
for i <- 0 to d
sort the elements according to ith place digits using countingSort
countingSort(array, d)
max <- find largest element among dth place elements
initialize count array with all zeros
for j <- 0 to size
find the total count of each unique digit in dth place of elements and
store the count at jth index in count array
for i <- 1 to max
find the cumulative sum and store it in count array itself
for j <- size down to 1
restore the elements to array
decrease count of each element restored by 1
C example
// Radix Sort in C Programming
#include <stdio.h>
// Function to get the largest element from an array
int getMax(int array[], int n) {
int max = array[0];
for (int i = 1; i < n; i++)
if (array[i] > max)
max = array[i];
return max;
}
// Using counting sort to sort the elements in the basis of significant places
void countingSort(int array[], int size, int place) {
int output[size + 1];
int max = (array[0] / place) % 10;
for (int i = 1; i < size; i++) {
if (((array[i] / place) % 10) > max)
max = array[i];
}
int count[max + 1];
for (int i = 0; i < max; ++i)
count[i] = 0;
// Calculate count of elements
for (int i = 0; i < size; i++)
count[(array[i] / place) % 10]++;
// Calculate cummulative count
for (int i = 1; i < 10; i++)
count[i] += count[i - 1];
// Place the elements in sorted order
for (int i = size - 1; i >= 0; i--) {
output[count[(array[i] / place) % 10] - 1] = array[i];
count[(array[i] / place) % 10]--;
}
for (int i = 0; i < size; i++)
array[i] = output[i];
}
// Main function to implement radix sort
void radixsort(int array[], int size) {
// Get maximum element
int max = getMax(array, size);
// Apply counting sort to sort elements based on place value.
for (int place = 1; max / place > 0; place *= 10)
countingSort(array, size, place);
}
// Print an array
void printArray(int array[], int size) {
for (int i = 0; i < size; ++i) {
printf("%d ", array[i]);
}
printf("\n");
}
// Driver code
int main() {
int array[] = {
121, 432, 564, 23, 1, 45, 788};
int n = sizeof(array) / sizeof(array[0]);
radixsort(array, n);
printArray(array, n);
}
the complexity
Since radix sort is a non-comparative sorting algorithm, it has advantages over comparative sorting algorithms.
For the base sort using counting sort as the intermediate stable sort, the time complexity is O(d(n+k)).
Here, d is the number of cycles, and O(n+k) is the time complexity of counting and sorting.
Therefore, the radix sort has linear time complexity, which is better than O(nlog n) of the comparison sorting algorithm.
If we use very large numbers or other base numbers, such as 32-bit or 64-bit numbers, although it can be executed in linear time, the intermediate sorting takes up a lot of space.
This makes the radix sort space inefficient. This is why this method is not used in software libraries.
Cardinality sort application
Cardinality sorting is applied in
- When the DC3 algorithm creates a suffix array.
- Where there is a large range of numbers.
Reference documents
[1]Parewa Labs Pvt. Ltd.Radix Sort Algorithm[EB/OL].https://www.programiz.com/dsa/radix-sort,2020-01-01.