1. Home
Data Structure

Data Structure Tutorial: Everything You Need to Know

Learn all about data structures with our comprehensive tutorial. Master the fundamentals and advance your skills in organizing and managing data efficiently.

  • 60
  • 14
right-top-arrow

Tutorial Playlist

58 Lessons
14

Mastering Radix Sort: Understanding its Efficiency and Implementation

Updated on 29/07/2024440 Views

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.

Overview

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.

The Radix Sort Algorithm

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

How the Radix Sort Works

  1. Initialization: Begin by creating ten buckets, numbered from 0 to 9, to hold elements based on their digit values.
  2. Bucket Distribution: Iterate through the array of elements, distributing each element into the respective bucket based on the value of the current digit being considered.
  3. Bucket Collection: After distributing all elements, collect them back from the buckets in the order they were placed, forming a partially sorted array.
  4. Repeat: Repeat the distribution and collection steps for each subsequent digit, proceeding from the least significant to the most significant digit.
  5. Completion: Once all digits have been processed, the array is fully sorted.

How is the Radix Sort Algorithm implemented?

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.

  • First, we will look at [132, 543, 783, 63, 7, 49, 898]. Radix Sort is used to sort it, as seen in the image below.
  • Determine the biggest, or largest, element in the array. Think of A as the maximum number of digits. We compute A because we have to go over every important place for every element.
  • In this array [132, 543, 783, 63, 7, 49, 898], 898 is the greatest number. Its numbers are three. Consequently, the loop needs to be repeated three times, reaching hundreds of locations.
  • Now, visit each important site individually. Using any reliable sorting method, arrange the numbers at each important location. For this, counting sort is required. Utilizing the unit place digits (A = 0), arrange the components.
  • Now arrange the components according to the tens place digits.

  • Sort the items lastly by hundreds of place digits.

You are going to look at the Radix Sort algorithm's pseudocode in the following lesson.

The Radix Sort Pseudocode

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 Radix Sort Algorithm's Performance

The Radix Sort time complexity

  1. Best Case Time Complexity

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).

  1. Worst-Case Time Complexity

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.

  1. Average Case Time Complexity

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).

Radix Sort Algorithm Stability

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.

Benefits of the Radix Sorting Technique

The Radix Sorting algorithm has the following benefits:

  • Quick when the array's elements range is narrow or when the keys are short.
  • It is utilized in techniques for building suffix arrays, like the DC3 and Manber algorithms.
  • It is a stable sorting technique because Radix Sort preserves the relative order of elements with equal values.

The Radix Sort Algorithm's drawbacks

Some drawbacks of the Radix Sorting method are as follows:

  • Because the Radix Sort algorithm is based on numbers or letters, it is less versatile than other sorts. As such, it needs to be rewritten for every distinct kind of data.
  • Compared to other sorting algorithms, the constant for Radix Sort is greater.
  • Compared to Quicksort, which is utilized for in-place sorting, it occupies more space.
  • If the processes are inefficient, Radix Sort could be slower than alternative sorting algorithms like merge sort and Quicksort. These actions include isolating the desired numbers, sub-inset lists, and deleting functions.

Radix Sort Algorithm Applications

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:

  1. Integer Sorting in Database Management Systems

In database management systems, Radix Sort is often employed to sort large sets of integers efficiently.

  1. String Sorting in Text Processing:

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.

  1. Digital Signal Processing

In digital signal processing (DSP), Radix Sort can be utilized to efficiently sort arrays of signal samples or process frequency bins.

  1. Sorting IP Addresses in Network Routing

Radix Sort is crucial in network routing protocols where IP addresses must be sorted for efficient routing table lookup.

  1. Image Processing

In image processing applications, Radix Sort can be used for sorting pixel values or processing image histograms.

  1. Data Compression Algorithms

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.

  1. Genomic Data Analysis

In bioinformatics, Radix Sort finds applications in sorting genomic data, such as DNA sequences or genetic markers.

  1. Time and Space Efficiency

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:

The Radix Sort in Python

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:

Conclusion

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.

FAQs

  1. Why is it called Radix Sort?

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.

  1. What is the heap and Radix Sort algorithm?

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.

  1. What is the best use of Radix Sort?

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.

  1. What is faster than Radix Sort?

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.

  1. Is Radix Sort divide and conquer?

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.

  1. What is the fastest sorting algorithm?

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.

Pavan Vadapalli

Pavan Vadapalli

Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working on solving problems of scale and l…Read More

Get Free Career Counselling
form image
+91
*
By clicking, I accept theT&Cand
Privacy Policy
image
right-top-arrowleft-top-arrow

upGrad Learner Support

Talk to our experts. We’re available 24/7.

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918045604032

Disclaimer

upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...