For working professionals
For fresh graduates
More
14. Radix Sort
20. AVL Tree
21. Red-Black Tree
23. Expression Tree
24. Adjacency Matrix
36. Greedy Algorithm
42. Tower of Hanoi
43. Stack vs Heap
47. Fibonacci Heap
49. Sparse Matrix
50. Splay Tree
In the realm of data structures and algorithms, Radix Sort is a powerful sorting technique known for its efficiency in handling large datasets. In this comprehensive guide, we delve into the intricacies of Radix Sort, exploring its algorithm, implementation, and practical examples.
Instead of directly comparing the components, Radix Sort divides them into buckets according to the value of each digit. Radix Sort determines the final order by repeatedly sorting the items from least to most significant by their significant digits.
Radix Sort is a non-comparative sorting algorithm that operates on the individual digits of numbers. Unlike comparison-based sorting algorithms such as quicksort or mergesort, Radix Sort exploits the data's inherent structure to achieve sorting efficiently. It is particularly useful when sorting integers or strings of fixed length, making it a valuable tool in various applications, including database management systems and digital signal processing.
When using a Radix Sort, each digit is sorted separately, starting with the least important and working to the most important.
Radix Sorting operates in a manner similar to sorting student names based on alphabetical order. In this instance, the 26 English alphabets formed 26 Radix. Students' names are arranged in the first pass based on the order in which their first letter appears. Following that, their names are arranged in ascending order based on the second letter in the second pass. Until the sorted list is found, the procedure keeps going.
This brings us to the Radix Sort algorithm:
RadixSort(arr)
max = largest element in the given array
d = number of digits in the largest element (or, max)
Now, create d buckets of size 0 - 9
for i -> 0 to d
sort the array elements using counting sort (or any stable sort) according to the digits at the ith place
Each digit is arranged in the Radix Sort algorithm from least to most significant.
The one's place digits would be sorted first, followed by the ten's place, and so on, in base 10. It utilizes a counting sort function to sort the data to every digit position.
This indicates that the first, tenth, and hundredth places of a three-digit base-10 number will be sorted using counting sort, producing a fully sorted list. A summary of the counting sort method is provided below.
Let's say you have an array with 8 entries. The items will first be sorted according to the value of the unit place. Subsequently, they will be sorted according to the value of the tenth place. This procedure will continue iterating until it reaches the final important point.
You are going to look at the Radix Sort algorithm's pseudocode in the following lesson.
function Radix_Sort(Array, p)
// Input: Array - the array to be sorted, p - the number of passes
for j = 1 to p do
// Iterate through each pass from 1 to p
// Initialize count_array to store the count of occurrences of each digit (0-9)
int count_array[10] = {0};
// Count occurrences of each digit at pass j
for i = 0 to n do
count_array[key_of(Array[i], pass j)]++
// Update count_array to hold cumulative counts
for k = 1 to 9 do
count_array[k] = count_array[k] + count_array[k-1]
// Construct the resulting array by placing elements in their sorted positions
for i = n-1 downto 0 do
result_array[ count_array[key_of(Array[i])] ] = Array[i]
// Construct the resulting array (result_array) by checking new Array[i] position from count_array[k]
count_array[key_of(Array[i])]--
// Update the main array with the sorted elements based on the current digit position
for i = 0 to n do
Array[i] = result_array[i]
// The main array Array[] now contains sorted numbers based on the current digit position.
// End of pass j
end for(j)
end function
Now that you know the Radix Sort algorithm's pseudocode, this tutorial will look at how well it performs.
The best-case scenario is when every element has the same number of digits. The best-case temporal complexity is O(a(n+b)). The time complexity is O. (a*n) if b = O(n).
The worst situation in Radix Sort is when every element has the same number of digits, except for one with a noticeably higher number than the others. The runtime is O. (n2) if the digit count of the largest element is equal to n.
In the average scenario, you considered the dispersion of the number of digits. Every digit can have up to 'd' distinct values, and there are 'p' passes. We can maintain n constant as Radix Sort does not depend on the input sequence.
The Radix Sort running time is T(n) = p(n+d). Considering the expectations of both parties and applying the linearity of expectation.
The average case time complexity of Radix Sort is O(p*(n+d)).
The Radix Sort Algorithm's Space Complexity
Because Radix Sort uses Counting sort, which utilizes auxiliary arrays of sizes n and k, where n is the number of items in the input array, and k is the greatest element among the elements in the input array's dth place (ones, tens, hundreds, and so forth), the space complexity of the Radix Sort is (n+k).
A reliable integer sorting technique based on subroutines is the Radix Sort algorithm. This sorting algorithm sorts a set of numbers without the need for comparisons. Individual digits with the same significant position and value are used to categorize keys.
The Radix Sorting algorithm has the following benefits:
Some drawbacks of the Radix Sorting method are as follows:
With its unique approach to sorting by individual digits, Radix Sort finds applications in various fields where efficient sorting of data is essential. Let's explore some of the key domains where the Radix Sort algorithm is applied:
In database management systems, Radix Sort is often employed to sort large sets of integers efficiently.
Radix Sort can also be used to sort strings of fixed length, making it useful in text-processing applications. For instance, in a word processor, Radix Sort can help alphabetize a list of words or sort paragraphs based on word count.
In digital signal processing (DSP), Radix Sort can be utilized to efficiently sort arrays of signal samples or process frequency bins.
Radix Sort is crucial in network routing protocols where IP addresses must be sorted for efficient routing table lookup.
In image processing applications, Radix Sort can be used for sorting pixel values or processing image histograms.
Radix Sort is utilized in data compression algorithms, where sorting plays a crucial role in identifying repetitive patterns and encoding data efficiently. For instance, in lossless compression techniques like Burrows-Wheeler Transform (BWT), Radix Sort is employed for sorting cyclic rotations of the input text, facilitating effective compression.
In bioinformatics, Radix Sort finds applications in sorting genomic data, such as DNA sequences or genetic markers.
Radix Sort's linear time complexity makes it particularly appealing for applications where sorting efficiency is critical. By exploiting the structure of data and sorting digits in parallel, Radix Sort achieves optimal time and space complexity, making it suitable for handling large datasets in real-time systems.
The Radix Sort in C
#include <stdio.h>
#include <stdlib.h>
int Max_value(int Array[], int n) {
int i;
int maximum = Array[0];
for (i = 1; i < n; i++) {
if (Array[i] > maximum)
maximum = Array[i];
}
return maximum;
}
void RadixSortalgorithm(int Array[], int n) {
int i, digitPlace = 1;
int result_array[n]; // resulting array
int largest = Max_value(Array, n); // Find the largest number to know number of digits
while (largest / digitPlace > 0) {
int count_array[10] = {0};
for (i = 0; i < n; i++) // Store the count of "keys" or digits in count[]
count_array[(Array[i] / digitPlace) % 10]++;
for (i = 1; i < 10; i++)
count_array[i] += count_array[i - 1];
for (i = n - 1; i >= 0; i--) { // Build the resulting array
result_array[count_array[(Array[i] / digitPlace) % 10] - 1] = Array[i];
count_array[(Array[i] / digitPlace) % 10]--;
}
for (i = 0; i < n; i++) // numbers according to current digit place
Array[i] = result_array[i];
digitPlace *= 10; // Move to next digit place
}
}
void displayArray(int Array[], int n) { // Function to print an array
int i;
for (i = 0; i < n; i++)
printf("%d ", Array[i]);
printf("\n");
}
int main() {
int array1[] = {20, 30, 40, 90, 60, 100, 50, 70};
int n = sizeof(array1) / sizeof(array1[0]);
printf("Unsorted Array is: ");
displayArray(array1, n);
RadixSortalgorithm(array1, n);
printf("Sorted Array is: ");
displayArray(array1, n);
return 0;
}
Output:
Unsorted Array is : 20 30 40 90 60 100 50 70
Sorted Array is: 20 30 40 50 60 70 90 100
-----------------------------------
Process exited after 0.04159 seconds with return value 0
Press any key to continue . . .
def counting_sort(arr, exp):
n = len(arr)
output = [0] * n
count = [0] * 10
for i in range(n):
index = arr[i] // exp
count[index % 10] += 1
for i in range(1, 10):
count[i] += count[i - 1]
i = n - 1
while i >= 0:
index = arr[i] // exp
output[count[index % 10] - 1] = arr[i]
count[index % 10] -= 1
i -= 1
for i in range(n):
arr[i] = output[i]
def Radix_sort(arr):
max_value = max(arr)
exp = 1
while max_value // exp > 0:
counting_sort(arr, exp)
exp *= 10
# Example
arr = [170, 45, 75, 90, 802, 24, 2, 66]
print("Unsorted Array:", arr)
Radix_sort(arr)
print("Sorted Array:", arr)
Output:
Unsorted Array: [170, 45, 75, 90, 802, 24, 2, 66]
Sorted Array: [2, 24, 45, 66, 75, 90, 170, 802]
The Radix Sort algorithm's versatility and efficiency make it a valuable tool in various domains, ranging from database management and digital signal processing to network routing and bioinformatics. Its ability to handle integer and string sorting tasks with linear time complexity makes it indispensable in many applications requiring efficient data sorting and processing.
It is called Radix Sort because it sorts the elements based on their individual digits (or Radix), moving from the least significant digit to the most significant digit.
Heap sort and Radix Sort are both sorting algorithms, but they are fundamentally different. Heap sort is a comparison-based sorting algorithm that builds a heap data structure and repeatedly extracts the maximum element. Radix Sort, on the other hand, is a non-comparative sorting algorithm that sorts elements based on their digits.
Radix Sort is best used for sorting integers or strings with fixed lengths, where the range of values is known and relatively small. It excels in scenarios where the length of the keys is limited and can be processed efficiently.
Radix Sort is often one of the fastest sorting algorithms, especially for sorting integers. However, in some cases, other sorting algorithms, like counting sort or bucket sort, maybe faster depending on the specific dataset and constraints.
No, Radix Sort is not a divide-and-conquer algorithm. It operates by distributing elements into buckets based on their digits and then collecting them back in a specific order without dividing the dataset into smaller subproblems like divide-and-conquer algorithms do.
The fastest sorting algorithm depends on various factors, such as the nature of the data, the size of the dataset, and the available hardware. The fastest sorting algorithm for general purposes is often considered quicksort, particularly when implemented with efficient optimizations like randomized pivot selection and median-of-three partitioning. However, the actual performance can vary based on the specific circumstances.
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.