• Category
  • >Python Programming

Radix Sort Algorithm in Python

  • Soumalya Bhattacharyya
  • Nov 07, 2023
  • Updated on: Sep 02, 2023
Radix Sort Algorithm in Python title banner

Welcome to our exploration of the Radix Sort algorithm in Python! Sorting lies at the heart of numerous computer science applications, and Radix Sort offers a unique approach that distinguishes it from traditional comparison-based sorting methods. In this blog post, we delve into the inner workings of Radix Sort, unraveling its mechanics and shedding light on its practical utility.

 

At its core, Radix Sort is a sorting algorithm that doesn't rely on the typical comparison operations between elements. Instead, it capitalizes on the inherent structure of the data being sorted, making it particularly well-suited for scenarios where elements are non-comparable, such as strings or complex objects. Our journey will take us through the fascinating concept of sorting by individual digits or characters, a process that involves iteratively distributing and gathering elements into buckets based on these positional values.

 

Throughout this blog, we'll provide you with a comprehensive Python implementation of the Radix Sort algorithm. You'll witness firsthand how to harness the power of Radix Sort to efficiently sort integers or strings. Moreover, we'll discuss the algorithm's time complexity, its advantages, and situations where it truly shines.

 

Whether you're new to sorting algorithms or looking to expand your repertoire, join us on this educational adventure as we demystify Radix Sort and showcase its role in the world of Python programming.

 

Radix Sort Algorithm

 

Radix sort is a non-comparative sorting algorithm that operates on integers or strings by processing individual digits or characters. Unlike comparison-based algorithms like QuickSort or MergeSort, Radix sort exploits the inherent structure of the data to achieve its sorting goal. 

 

The algorithm's fundamental idea is to sort elements based on their digits or characters from least significant to most significant. It performs this sorting process iteratively for each digit or character, using auxiliary data structures called "buckets." During each iteration, the elements are distributed into these buckets based on the current digit's value. After each iteration, the elements are collected back in a specific order, and the process continues until all digits or characters have been processed.

 

Radix sort is known for its linear time complexity in certain scenarios, making it particularly suitable for large datasets where the range of values is not excessively large. It's especially useful for sorting non-comparable data types, such as strings or custom objects, as long as an appropriate method of extracting digits or characters is available. However, Radix sort may not always outperform other algorithms due to its overhead in terms of memory usage and multiple passes over the data. Nevertheless, its unique approach and efficiency in specific cases make it an essential tool in the realm of sorting algorithms.

 

How does the Radix Sort Algorithm work?

 

Radix Sort is a fascinating non-comparative sorting algorithm that operates by processing digits or characters of elements from least significant to most significant. This approach sets it apart from traditional comparison-based sorting algorithms like QuickSort or MergeSort, making it particularly suitable for situations where the data being sorted is non-comparable or has a well-defined structure, such as integers or strings.

 

The fundamental principle underlying Radix Sort is to sort elements based on their positional values. The algorithm achieves this through a series of iterations, each focusing on a specific digit or character position. During each iteration, the elements are distributed into buckets based on the value of the digit at that particular position. This digit acts as a key, dictating which bucket the element should be placed into. After each iteration, the elements are collected back in the order dictated by the buckets, and the process continues with the next digit position.

 

To put this into perspective, consider sorting a list of integers: [170, 45, 75, 90, 802, 24, 2, 66]. Radix Sort would start by looking at the least significant digit (unit's place), creating buckets labeled from 0 to 9. The elements would be distributed into these buckets based on the digit's value, resulting in [802, 2], [90], [170], [75], [45], [66], [24], and an empty bucket for 9. After this step, the elements are gathered back in the order of the buckets, giving [802, 2, 90, 170, 75, 45, 66, 24].

 

The process then moves to the next digit position (tens' place), and the elements are redistributed into buckets once again. This process continues iteratively until all digit positions have been processed. Remarkably, each iteration preserves the order of elements with the same digit values, making Radix Sort a stable sorting algorithm.

 

Radix Sort's efficiency lies in its linear time complexity for certain scenarios. However, it's important to note that the number of iterations is determined by the number of digits in the largest element. Consequently, the algorithm's performance depends on the range of values and the number of elements being sorted. Radix Sort also comes with a memory overhead due to the creation of buckets and auxiliary arrays.

 

Complexity Analysis of Radix Sort:

 

The complexity analysis of Radix Sort involves understanding both its time complexity and space complexity. Let's delve into each aspect:

 

  1. Time Complexity:

 

The time complexity of Radix Sort is O(nk), where n is the number of elements in the array to be sorted, and k is the number of digits in the maximum element. It might seem counterintuitive that Radix Sort is considered linear despite having nested iterations. However, the crucial point is that the number of iterations (k) is not dependent on the size of the array (n) but on the number of digits in the largest element. This makes Radix Sort particularly efficient when the range of values is not excessively large, and the number of digits remains relatively small.

 

  1. Space Complexity:

 

The space complexity of Radix Sort is O(n + k), where n is the number of elements in the array, and k is the range of possible digit values (typically 10 for base-10 numbers). This complexity arises from the need to create auxiliary data structures like buckets and temporary arrays to store elements during each iteration. The space required for these structures increases with the number of elements and the number of possible digit values. However, in most practical scenarios, k is relatively small compared to n, making the space complexity linear as well.

 

Example of Radix sort in python

 

def radix_sort(arr):

    max_num = max(arr)  # Find the maximum element to determine the number of digits

    

    # Perform counting sort for every digit place (ones, tens, hundreds, etc.)

    exp = 1

    while max_num // exp > 0:

        counting_sort(arr, exp)

        exp *= 10



def counting_sort(arr, exp):

    n = len(arr)

    output = [0] * n

    count = [0] * 10

    

    # Count the occurrences of each digit at the current place

    for num in arr:

        count[(num // exp) % 10] += 1

    

    # Update count array to store the actual position of elements

    for i in range(1, 10):

        count[i] += count[i - 1]

    

    # Build the output array based on the sorted positions

    i = n - 1

    while i >= 0:

        digit = (arr[i] // exp) % 10

        output[count[digit] - 1] = arr[i]

        count[digit] -= 1

        i -= 1

    

    # Copy the sorted elements back to the original array

    for i in range(n):

        arr[i] = output[i]



# Example usage

arr = [170, 45, 75, 90, 802, 24, 2, 66]

radix_sort(arr)

print("Sorted array:", arr)

 

In this example, the radix_sort function iterates through each digit and calls the counting_sort function to perform a stable sort based on the current digit. The counting_sort function uses the counting sort technique to sort the elements based on the digit's value at the current place. The exp variable is used to keep track of the current digit place (1, 10, 100, etc.).

 

Comparisons of Radix Sort with other comparative algorithms

 

Radix Sort stands out from other comparison-based sorting algorithms due to its unique approach and specific use cases. Here's a comparison of Radix Sort with some commonly used comparison-based algorithms:

 

QuickSort:

 

  • Radix Sort is non-comparative, while QuickSort is comparison-based.

  • Radix Sort doesn't rely on comparisons; it sorts by individual digits or characters.

  • QuickSort generally has better average-case performance, but its worst-case time complexity is higher (O(n^2)).

  • Radix Sort's time complexity is O(nk), where k is the number of digits, making it efficient for limited digit ranges.

 

MergeSort:

 

  • MergeSort is comparison-based, focusing on splitting and merging subarrays.

  • Radix Sort's time complexity can be better in some cases, especially for small arrays with large elements.

  • MergeSort's stable property ensures equal elements maintain their order, while Radix Sort's stability depends on the stability of its sub-sorting algorithm.

 

HeapSort:

 

  • HeapSort is comparison-based and constructs a binary heap for sorting.

  • Radix Sort is more suitable for small keys or keys with a bounded number of digits.

  • HeapSort's time complexity is consistent at O(n log n), while Radix Sort's time complexity depends on the digit count.

 

BubbleSort:

 

  • BubbleSort is a simple comparison-based algorithm with high time complexity.

  • Radix Sort's efficiency makes it more suitable for larger datasets.

  • BubbleSort has a worst-case time complexity of O(n^2), while Radix Sort's worst-case is O(nk).

 

InsertionSort:

 

  • InsertionSort is comparison-based and works well for small lists.

  • Radix Sort's performance shines when the range of values is not large.

  • InsertionSort's best-case time complexity is O(n), while Radix Sort's complexity depends on the digit count.

 

Radix Sort is distinctive due to its focus on digit or character processing rather than comparisons. It excels in situations where the range of values is bounded and has specific use cases like sorting integers or strings efficiently. While it might not be the best choice for all scenarios, its unique strengths make it a valuable addition to the sorting algorithm landscape.

 

Applications of Radix Sort Algorithm

 

Radix Sort's unique characteristics make it suitable for various specific applications where its advantages can be leveraged effectively. Some notable applications of Radix Sort include:

 

  1. Radix Sort is particularly efficient when sorting integers or strings with a fixed number of digits or characters. Its linear time complexity for such cases can outperform comparison-based algorithms.

  2. When dealing with strings, especially when each string has the same length, Radix Sort can be a powerful sorting technique. It can sort strings lexicographically by processing characters at different positions.

  3. IP addresses consist of four groups of numbers, each ranging from 0 to 255. Radix Sort can be applied to efficiently sort IP addresses by each group, which corresponds to sorting by segments of digits.

  4. Dates and timestamps can be represented as numbers with year, month, day, hour, minute, and second components. Radix Sort can sort these representations by each component, enabling chronological ordering.

  5. Radix Sort is efficient when sorting data with a limited range of values. It can be useful for large datasets that fit this criterion, even if the data types are not integers.

  6. In scenarios where data exceeds available memory, external sorting techniques are required. Radix Sort's inherent characteristic of sorting data in fixed-size segments makes it suitable for external sorting tasks.

  7. Radix Sort is stable by nature, meaning equal elements maintain their original order. This property is beneficial in various applications where stability is essential.

  8. Radix Sort can be used in various image processing tasks, such as sorting pixel values based on intensity or color components.

  9. Radix Sort can be applied to efficiently sort data in databases, especially when dealing with structured data formats like fixed-length records.

  10. When needing to sort data using multiple criteria (e.g., sorting students by grade and then by name), Radix Sort can be applied iteratively on each criterion.

 

Conclusion:

 

Radix Sort in Python offers a distinctive approach to sorting that capitalizes on digits or characters. Its linear time complexity for limited digit ranges and suitability for non-comparable data make it a valuable addition to sorting algorithms, especially in scenarios where traditional methods may fall short.

Latest Comments

  • mary james

    Nov 07, 2023

    HOW I RECOVER MY LOST INVESTMENT FUND'S FROM FAKE INVESTOR'S ONLINE 2023 I was scammed over ( $345,000 ) by someone I met online on a fake investment project. I started searching for help legally to recover my money and I came across a lot of Testimonies about ETHICREFINANCE Recovery Expects. I contacted them providing the necessary information's and it took the experts about 27hours to locate and help recover my stolen funds. I am so relieved and the best part was, the scammer was located and arrested by local authorities in his region. I hope this help as many out there who are victims and have lost to these fake online investment scammers.I strongly recommend their professional services for assistance with swift and efficient recovery. They can reached through the link below. Email Address: ethicsrefinance @g-mail*com WhatsApp: +1 (719) 642-8467 THEY OFFER THE FOLLOWING SERVICES * RECOVER LOST/STOLEN CRYPTO * BLANK ATM CARD * PAYPAL HACK TRANFER * CASH APP FLIP * WESTERN UNION FLIP * BANK WIRE TRANSFER * ANY HACK SERVICES YOU NEED…E.T.C

  • brenwright30

    May 11, 2024

    THIS IS HOW YOU CAN RECOVER YOUR LOST CRYPTO? Are you a victim of Investment, BTC, Forex, NFT, Credit card, etc Scam? Do you want to investigate a cheating spouse? Do you desire credit repair (all bureaus)? Contact Hacker Steve (Funds Recovery agent) asap to get started. He specializes in all cases of ethical hacking, cryptocurrency, fake investment schemes, recovery scam, credit repair, stolen account, etc. Stay safe out there! Hackersteve911@gmail.com https://hackersteve.great-site.net/