Radix Sort
In-Place Sorting
Algorithm
Data Structures
Computer Science

In-Place Radix Sort

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

In-place radix sort is a fascinating variation of the radix sort algorithm that is designed to be performed without needing additional storage space beyond the input data. This approach enhances the efficiency of radix sort in terms of space complexity, which is a significant consideration in many applications. Here, we delve into its technicalities, workings, and performance.

Understanding Radix Sort

Radix sort is a non-comparative sorting algorithm that sorts numbers by processing individual digits. It works by distributing numbers into buckets according to their digits, starting from the least significant digit (LSD) to the most significant digit (MSD). This bucket-based approach is repeated until all digits are processed, resulting in a sorted list.

Basic Concept

In radix sort, the array is divided into k positional buckets, where k is determined by the number of unique digits (e.g., 0-9 for decimal numbers). For each pass, numbers are grouped by the relevant digit and collected, beginning from the LSD up to the MSD.

In-place Radix Sort

An in-place radix sort reduces the need for additional storage during execution. This is typically achieved by applying a variation of a counting sort internally, which directly rearranges the input array without using auxiliary storage for buckets.

Key Challenges

  • In-place rearrangement: Modifying traditional bucket sort techniques to fit within the original array.
  • Efficiency: Maintaining the time complexity benefit of radix sort, ideally O(d×(n+k))\mathcal{O}(d \times (n + k)), where d is the number of digits in the longest number, n is the number of elements, and k is the base of the number system used.
  • Stable sorting: Ensuring that the sort maintains stability, where equal elements retain their original relative order.

Technique

  1. Digit Extraction: Extract digits using modular arithmetic:
    For a given number x and base b, the iith digit can be extracted using:
    ith_digit=xbimodbi_{th\_digit} = \left\lfloor \frac{x}{b^i} \right\rfloor \bmod b
  2. Counting Sort for Digits:
    • Count occurrences of each digit.
    • Modify the count array so that each element indicates the position of the first occurrence of that particular digit.
    • Iterate over the array, placing each element in its correct position determined by the count array.
    • Decrement the count, moving in-place.
  3. Iterate Over Digits: Process each digit, from least significant to most significant, using the in-place approach described above.

Complexity Analysis

  • Time Complexity: O(d×(n+k))\mathcal{O}(d \times (n + k))
    The time complexity remains linear with respect to the number of elements, n, given that d (number of digits) and k (base) are constants.
  • Space Complexity: O(1)\mathcal{O}(1) extra space is required since the sorting is in-place.

Example

Imagine sorting a list of four-digit numbers, such as [1234, 6254, 3209, 5156]. Here's a simplified breakdown:

  • Digit Count: Start with the LSD (units place, e.g., 4 in 1234).
  • Ordering: Rearrange based on this digit using an in-place method.
  • Repeat: Continue for the tens, hundreds, and finally thousands digit.

Advantages and Disadvantages

AspectDescription
ProsEfficient in terms of space (in-place, O(1)\mathcal{O}(1)) Maintains the linear time complexity characteristic of radix sort
ConsComplexity in implementation due to intricate in-place mechanism Not suited for all types of data, especially with very large ranges of numbers or alphabetic data

Extensions and Applications

In-place radix sort is most beneficial in applications with a constrained memory environment or when processing very large datasets where in-place operations are desirable. Examples include memory-efficient databases or embedded systems.


In conclusion, in-place radix sort is a valuable extension of the traditional radix sort, offering benefits in space efficiency while preserving the algorithm's time efficiency. Mastery of this technique, however, requires careful consideration of the in-place procedures to maintain both efficiency and correctness.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.